leetcode 148. Sort List 链表归并排序
2017-09-18 09:26
429 查看
Sort a linked list in O(n log n) time using constant space complexity.
本题就是考察的是链表的归并排序。
代码如下:
下面是C++的做法,就是一个简单的归并排序的实现
代码如下:
本题就是考察的是链表的归并排序。
代码如下:
/*class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }*/ public class Solution { public ListNode sortList(ListNode head) { return mergeSort(head); } ListNode mergeSort(ListNode head) { if(head==null || head.next==null) return head; else { ListNode head1=head; ListNode head2=getMidListNode(head); ListNode nextHead=head2.next; head2.next=null; ListNode tmp1=mergeSort(head1); ListNode tmp2=mergeSort(nextHead); return merge(tmp1,tmp2); } } ListNode merge(ListNode head1, ListNode head2) { ListNode fin=new ListNode(-1); ListNode cur=fin; while(head1!=null && head2!=null) { if(head1.val <= head2.val) { //这个实在链表结尾添加结点 cur.next=head1; head1=head1.next; cur=cur.next; cur.next=null; }else { cur.next=head2; head2=head2.next; cur=cur.next; cur.next=null; } } if(head1!=null) cur.next=head1; if(head2!=null) cur.next=head2; return fin.next; } ListNode getMidListNode(ListNode head) { ListNode fast=head,slow=head; while(slow!=null && fast!=null && fast.next!=null && fast.next.next!=null) { slow=slow.next; fast=fast.next.next; } return slow; } }
下面是C++的做法,就是一个简单的归并排序的实现
代码如下:
#include <iostream> #include <stack> #include <queue> using namespace std; /* struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; */ class Solution { public: ListNode* sortList(ListNode* head) { return mergeSort(head); } ListNode* mergeSort(ListNode* head) { if (head == NULL || head->next == NULL) return head; else { ListNode* h1 = head; ListNode* tmp = getMid(head); ListNode* h2 = tmp->next; tmp->next = NULL; h1 = mergeSort(h1); h2 = mergeSort(h2); return merge(h1, h2); } } ListNode* merge(ListNode* h1, ListNode* h2) { ListNode* fin = new ListNode(-1); ListNode* i = fin; while (h1 != NULL && h2 != NULL) { if (h1->val <= h2->val) { i->next = h1; h1 = h1->next; i = i->next; } else { i->next = h2; h2 = h2->next; i = i->next; } } if (h1 != NULL) i->next = h1; if (h2 != NULL) i->next = h2; return fin->next; } ListNode* getMid(ListNode* head) { ListNode* fast = head; ListNode* slow = head; while (fast != NULL && fast->next != NULL && fast->next->next != NULL) { slow = slow->next; fast = fast->next->next; } return slow; } };
相关文章推荐
- leetcode_效率题解_148. Sort List_(链表归并排序)
- leetcode 148. Sort List 链表 归并排序 和 快排
- [LeetCode] 148. Sort List 链表排序
- 【LeetCode】Sort List 解题报告(对链表进行归并排序)
- LeetCode Sort List 链表的归并排序
- 148. Sort List--单链表自底向上归并排序
- 148. Sort List 链表归并排序
- 148. Sort List(链表的归并排序,用快慢指针来partition)
- LeetCode 148. Sort List(链表排序)
- LeetCode 148.Sort List 链表排序
- [C++]LeetCode: 125 Sort List (归并排序链表)
- leetcode:Sort List(链表的归并排序)
- [leetcode] 134 Sort List (链表 & 归并排序)
- Leetcode Sort List 链表归并排序
- LeetCode 148. Sort List--O(nlogn)时间复杂度和常数空间复杂度给链表排序
- LeetCode 148. Sort List 解题报告(归并排序小结)
- LeetCode 148. Sort List 单链表排序 Python Solution
- leetcode:Sort List(一个链表的归并排序)
- 【leetcode】【单链表】【148】Sort List
- LeetCode Insertion Sort List 链表的插入排序