LeetCode之“链表”:Sort List

  题目链接

  题目要求:

  Sort a linked list in O(n log n) time using constant space complexity.

  满足O(n log n)时间复杂度的有快排、归并排序、堆排序。在这里采用的是归并排序(空间复杂度O(log n)),具体程序如下:

 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* merge(ListNode* l1, ListNode* l2)
12     {
13         ListNode *dummy = new ListNode(0);
14         ListNode *head = nullptr, *start = dummy;
15         while(l1 && l2)
16         {
17             if(l1->val < l2->val)
18             {
19                 start->next = l1;
20                 l1 = l1->next;
21             }
22             else
23             {
24                 start->next = l2;
25                 l2 = l2->next;
26             }
27             start = start->next;
28         }
29
30         if(!l1)
31             start->next = l2;
32         else if(!l2)
33             start->next = l1;
34
35         head = dummy->next;
36         delete dummy;
37         dummy = nullptr;
38
39         return head;
40     }
41
42     ListNode* mergeSort(ListNode* head)
43     {
44         if(!head->next)
45             return head;
46
47         ListNode *slow = head, *fast = head;
48         ListNode *preNode = slow;
49         while(fast && fast->next)
50         {
51             preNode = slow;
52             slow = slow->next;
53             fast = fast->next->next;
54         }
55         preNode->next = nullptr;
56
57         ListNode *left = mergeSort(head);
58         ListNode *right = mergeSort(slow);
59         return merge(left, right);
60     }
61
62     ListNode* sortList(ListNode* head) {
63         if(!head || !head->next)
64             return head;
65
66         return mergeSort(head);
67     }
68 };
时间: 2024-10-13 11:38:34

LeetCode之“链表”:Sort List的相关文章

【LeetCode】Insertion Sort List

题目 Sort a linked list using insertion sort. 解答 链表无法像数组那样从后往前依次比较插入,只能从前往后:在链表首部添加一个哨兵可以稍微简化下代码,代码如下: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * }

[LeetCode 题解]: Insertion Sort List

Sort a linked list using insertion sort. 题目要求:链表的插入排序,由于没有时间复杂度的要求,可以直接循环操作. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode

[LeetCode系列]链表环探测问题II

给定一个链表头, 探测其是否有环, 如果没有返回NULL, 如果有返回环开始的位置. 环开始的位置定义为被两个指针指向的位置. 算法描述: 1. 快慢指针遍历, 如果到头说明无环返回NULL, 如果相遇说明有环, 进入2. 2. 慢指针回到起点, 快慢指针每次移动一格直到相遇, 返回快指针/慢指针. 代码: 1 class Solution { 2 public: 3 ListNode *detectCycle(ListNode *head) { 4 if (!head || !head->ne

LeetCode OJ - Insertion Sort List

题目: Sort a linked list using insertion sort. 解题思路: 假设 list[1..i]是排好序的,找到第i+1个元素应该插入的位置及其前驱,然后将其插入. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class S

【算法题 14 LeetCode 147 链表的插入排序】

算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def insertionSortList(self, head): """ :type head: ListNode

【leetcode】905. Sort Array By Parity

题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数组头部,否则追加到尾部. 代码如下: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ res =

LeetCode 单链表专题 (一)

目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Partition List \([82]\) Remove Duplicates from Sorted List II \([61]\) Rotate List \([19]\) Remove Nth Node From End of List LeetCode 单链表专题 <c++> \([2]\)

LeetCode 206 链表 Reverse Linked List

LeetCode 206 链表 Reverse Linked List Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe

[LeetCode]85. Insertion Sort List链表插入排序

Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解法:设置3个指针:应插入位置的前一个节点first.当前处理节点third和其前一个节点second,设置辅助节点help指向头节点.然后从头节点的next节点开始遍历,一个个插入正确位置即可.注意在插入到新位置之前需要链接好second和third->next,防止链表断开. /** * Definitio