LeetCode: Sort List

merge sort

 1 /**

 2  * Definition for singly-linked list.

 3  * struct ListNode {

 4  *     int val;

 5  *     ListNode *next;

 6  *     ListNode(int x) : val(x), next(NULL) {}

 7  * };

 8  */

 9 class Solution {

10 public:

11     ListNode *mergesort(ListNode *p, ListNode *q) {

12         ListNode *head = NULL;

13         ListNode *cur = NULL;

14         while (p && q) {

15             if (p->val < q->val) {

16                 if (!head) head = cur = p;

17                 else {

18                     cur->next = p;

19                     cur = cur->next;

20                 }

21                 p = p->next;

22             }

23             else {

24                 if (!head) head = cur = q;

25                 else {

26                     cur->next = q;

27                     cur = cur->next;

28                 }

29                 q = q->next;

30             }

31         }

32         if (p) cur->next = p;

33         else cur->next = q;

34         return head;

35     }

36     ListNode *sortList(ListNode *head) {

37         if (!head || !head->next) return head;

38         ListNode *p, *q;

39         p = q = head;

40         while (q && q->next) {

41             q = q->next->next;

42             if (!q) break;

43             p = p->next;

44         }

45         q = p->next;

46         p->next = NULL;

47         p = sortList(head);

48         q = sortList(q);

49         return mergesort(p, q);

50     }

51 };

 C#

 1 /**

 2  * Definition for singly-linked list.

 3  * public class ListNode {

 4  *     public int val;

 5  *     public ListNode next;

 6  *     public ListNode(int x) { val = x; }

 7  * }

 8  */

 9 public class Solution {

10     public ListNode SortList(ListNode head) {

11         if (head == null || head.next == null) return head;

12         ListNode p = head, q = head;

13         while (q != null && q.next != null) {

14             q = q.next.next;

15             if (q == null) break;

16             p = p.next;

17         }

18         q = p.next;

19         p.next = null;

20         p = SortList(head);

21         q = SortList(q);

22         return mergesort(p, q);

23     }

24     public ListNode mergesort(ListNode p, ListNode q) {

25         ListNode head = null;

26         ListNode cur = null;

27         while (p != null && q != null) {

28             if (p.val < q.val) {

29                 if (head == null) head = cur = p;

30                 else {

31                     cur.next = p;

32                     cur = cur.next;

33                 }

34                 p = p.next;

35             }

36             else {

37                 if (head == null) head = cur = q;

38                 else {

39                     cur.next = q;

40                     cur = cur.next;

41                 }

42                 q = q.next;

43             }

44         }

45         if (p != null) cur.next = p;

46         else cur.next = q;

47         return head;

48     }

49 }
View Code

 

你可能感兴趣的:(LeetCode)