[Leetcode] Sort list (sorting)

Source: Internet
Author: User

This question requires two linked lists to be sorted by merging. The basic idea is to split and merge them.

The Merged Code is clearly described in the merge two sorted list. So the code is provided here.

public ListNode merge(ListNode l1, ListNode l2) {ListNode helper = new ListNode(0);ListNode runner = helper;while (l1 != null && l2 != null) {if (l1.val < l2.val) {runner.next = l1;l1 = l1.next;runner = runner.next;} else {runner.next = l2;l2 = l2.next;runner = runner.next;}if (l1 != null)runner.next = l1;if (l2 != null)runner.next = l2;}return helper.next;}

The idea of separation is very important. In the array, it ends when only one element is left, and the same is true for the chain table.

The method for judging only one element in the array is if (Left = right)

The method for determining only one element in the linked list is if (head. Next = NULL)

The Division Method in the array is

int middle = (left + right) / 2;mergeSort(array, left, middle);mergeSort(array, middle + 1, right);merge(array, left, middle, right);

How can I find the middle in the linked list? At this time, we think of the walker_runner technology mentioned above to find the point.

ListNode walker = head;ListNode runner = head;while (runner.next != null && runner.next.next != null) {walker = walker.next;runner = runner.next.next;}ListNode head2 = walker.next;walker.next = null;head = mergeSort(head);head2 = mergeSort(head2);return merge(head, head2);

The complete code is provided below

public ListNode sortList(ListNode head) {if (head == null)return head;return mergeSort(head);}public ListNode mergeSort(ListNode head) {if (head.next == null)return head;ListNode walker = head;ListNode runner = head;while (runner.next != null && runner.next.next != null) {walker = walker.next;runner = runner.next.next;}ListNode head2 = walker.next;walker.next = null;head = mergeSort(head);head2 = mergeSort(head2);return merge(head, head2);}public ListNode merge(ListNode l1, ListNode l2) {ListNode helper = new ListNode(0);ListNode runner = helper;while (l1 != null && l2 != null) {if (l1.val < l2.val) {runner.next = l1;l1 = l1.next;runner = runner.next;} else {runner.next = l2;l2 = l2.next;runner = runner.next;}if (l1 != null)runner.next = l1;if (l2 != null)runner.next = l2;}return helper.next;}



[Leetcode] Sort list (sorting)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.