diff --git a/solution/0100-0199/0148.Sort List/README.md b/solution/0100-0199/0148.Sort List/README.md index cf886e6900730..688982289827b 100644 --- a/solution/0100-0199/0148.Sort List/README.md +++ b/solution/0100-0199/0148.Sort List/README.md @@ -67,7 +67,15 @@ tags: -### 方法一 +### 方法一:归并排序 + +我们可以用归并排序的思想来解决。 + +首先,我们利用快慢指针找到链表的中点,将链表从中点处断开,形成两个独立的子链表 $\textit{l1}$ 和 $\textit{l2}$。 + +然后,我们递归地对 $\textit{l1}$ 和 $\textit{l2}$ 进行排序,最后将 $\textit{l1}$ 和 $\textit{l2}$ 合并为一个有序链表。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是链表的长度。 @@ -80,26 +88,27 @@ tags: # self.val = val # self.next = next class Solution: - def sortList(self, head: ListNode) -> ListNode: + def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: if head is None or head.next is None: return head slow, fast = head, head.next while fast and fast.next: - slow, fast = slow.next, fast.next.next - t = slow.next + slow = slow.next + fast = fast.next.next + l1, l2 = head, slow.next slow.next = None - l1, l2 = self.sortList(head), self.sortList(t) + l1, l2 = self.sortList(l1), self.sortList(l2) dummy = ListNode() - cur = dummy + tail = dummy while l1 and l2: if l1.val <= l2.val: - cur.next = l1 + tail.next = l1 l1 = l1.next else: - cur.next = l2 + tail.next = l2 l2 = l2.next - cur = cur.next - cur.next = l1 or l2 + tail = tail.next + tail.next = l1 or l2 return dummy.next ``` @@ -126,23 +135,23 @@ class Solution { slow = slow.next; fast = fast.next.next; } - ListNode t = slow.next; + ListNode l1 = head, l2 = slow.next; slow.next = null; - ListNode l1 = sortList(head); - ListNode l2 = sortList(t); + l1 = sortList(l1); + l2 = sortList(l2); ListNode dummy = new ListNode(); - ListNode cur = dummy; + ListNode tail = dummy; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { - cur.next = l1; + tail.next = l1; l1 = l1.next; } else { - cur.next = l2; + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 == null ? l2 : l1; + tail.next = l1 != null ? l1 : l2; return dummy.next; } } @@ -164,30 +173,33 @@ class Solution { class Solution { public: ListNode* sortList(ListNode* head) { - if (!head || !head->next) return head; - auto* slow = head; - auto* fast = head->next; + if (!head || !head->next) { + return head; + } + ListNode* slow = head; + ListNode* fast = head->next; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } - auto* t = slow->next; + ListNode* l1 = head; + ListNode* l2 = slow->next; slow->next = nullptr; - auto* l1 = sortList(head); - auto* l2 = sortList(t); - auto* dummy = new ListNode(); - auto* cur = dummy; + l1 = sortList(l1); + l2 = sortList(l2); + ListNode* dummy = new ListNode(); + ListNode* tail = dummy; while (l1 && l2) { if (l1->val <= l2->val) { - cur->next = l1; + tail->next = l1; l1 = l1->next; } else { - cur->next = l2; + tail->next = l2; l2 = l2->next; } - cur = cur->next; + tail = tail->next; } - cur->next = l1 ? l1 : l2; + tail->next = l1 ? l1 : l2; return dummy->next; } }; @@ -211,25 +223,27 @@ func sortList(head *ListNode) *ListNode { for fast != nil && fast.Next != nil { slow, fast = slow.Next, fast.Next.Next } - t := slow.Next + l1 := head + l2 := slow.Next slow.Next = nil - l1, l2 := sortList(head), sortList(t) + l1 = sortList(l1) + l2 = sortList(l2) dummy := &ListNode{} - cur := dummy + tail := dummy for l1 != nil && l2 != nil { if l1.Val <= l2.Val { - cur.Next = l1 + tail.Next = l1 l1 = l1.Next } else { - cur.Next = l2 + tail.Next = l2 l2 = l2.Next } - cur = cur.Next + tail = tail.Next } if l1 != nil { - cur.Next = l1 + tail.Next = l1 } else { - cur.Next = l2 + tail.Next = l2 } return dummy.Next } @@ -251,32 +265,31 @@ func sortList(head *ListNode) *ListNode { */ function sortList(head: ListNode | null): ListNode | null { - if (head == null || head.next == null) return head; - // 快慢指针定位中点 - let slow: ListNode = head, - fast: ListNode = head.next; - while (fast != null && fast.next != null) { - slow = slow.next; + if (head === null || head.next === null) { + return head; + } + let [slow, fast] = [head, head.next]; + while (fast !== null && fast.next !== null) { + slow = slow.next!; fast = fast.next.next; } - // 归并排序 - let mid: ListNode = slow.next; + let [l1, l2] = [head, slow.next]; slow.next = null; - let l1: ListNode = sortList(head); - let l2: ListNode = sortList(mid); - let dummy: ListNode = new ListNode(); - let cur: ListNode = dummy; - while (l1 != null && l2 != null) { + l1 = sortList(l1); + l2 = sortList(l2); + const dummy = new ListNode(); + let tail = dummy; + while (l1 !== null && l2 !== null) { if (l1.val <= l2.val) { - cur.next = l1; + tail.next = l1; l1 = l1.next; } else { - cur.next = l2; + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 == null ? l2 : l1; + tail.next = l1 ?? l2; return dummy.next; } ``` @@ -357,32 +370,31 @@ impl Solution { * @return {ListNode} */ var sortList = function (head) { - if (!head || !head.next) { + if (head === null || head.next === null) { return head; } - let slow = head; - let fast = head.next; - while (fast && fast.next) { + let [slow, fast] = [head, head.next]; + while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; } - let t = slow.next; + let [l1, l2] = [head, slow.next]; slow.next = null; - let l1 = sortList(head); - let l2 = sortList(t); + l1 = sortList(l1); + l2 = sortList(l2); const dummy = new ListNode(); - let cur = dummy; - while (l1 && l2) { + let tail = dummy; + while (l1 !== null && l2 !== null) { if (l1.val <= l2.val) { - cur.next = l1; + tail.next = l1; l1 = l1.next; } else { - cur.next = l2; + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 || l2; + tail.next = l1 ?? l2; return dummy.next; }; ``` @@ -403,37 +415,31 @@ var sortList = function (head) { */ public class Solution { public ListNode SortList(ListNode head) { - if (head == null || head.next == null) - { + if (head == null || head.next == null) { return head; } ListNode slow = head, fast = head.next; - while (fast != null && fast.next != null) - { + while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } - ListNode t = slow.next; + ListNode l1 = head, l2 = slow.next; slow.next = null; - ListNode l1 = SortList(head); - ListNode l2 = SortList(t); + l1 = SortList(l1); + l2 = SortList(l2); ListNode dummy = new ListNode(); - ListNode cur = dummy; - while (l1 != null && l2 != null) - { - if (l1.val <= l2.val) - { - cur.next = l1; + ListNode tail = dummy; + while (l1 != null && l2 != null) { + if (l1.val <= l2.val) { + tail.next = l1; l1 = l1.next; - } - else - { - cur.next = l2; + } else { + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 == null ? l2 : l1; + tail.next = l1 != null ? l1 : l2; return dummy.next; } } diff --git a/solution/0100-0199/0148.Sort List/README_EN.md b/solution/0100-0199/0148.Sort List/README_EN.md index f6811cf00d549..95ec46d8cb436 100644 --- a/solution/0100-0199/0148.Sort List/README_EN.md +++ b/solution/0100-0199/0148.Sort List/README_EN.md @@ -61,7 +61,15 @@ tags: -### Solution 1 +### Solution 1: Merge Sort + +We can use the merge sort approach to solve this problem. + +First, we use the fast and slow pointers to find the middle of the linked list and break the list from the middle to form two separate sublists $\textit{l1}$ and $\textit{l2}$. + +Then, we recursively sort $\textit{l1}$ and $\textit{l2}$, and finally merge $\textit{l1}$ and $\textit{l2}$ into a sorted linked list. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the linked list. @@ -74,26 +82,27 @@ tags: # self.val = val # self.next = next class Solution: - def sortList(self, head: ListNode) -> ListNode: + def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: if head is None or head.next is None: return head slow, fast = head, head.next while fast and fast.next: - slow, fast = slow.next, fast.next.next - t = slow.next + slow = slow.next + fast = fast.next.next + l1, l2 = head, slow.next slow.next = None - l1, l2 = self.sortList(head), self.sortList(t) + l1, l2 = self.sortList(l1), self.sortList(l2) dummy = ListNode() - cur = dummy + tail = dummy while l1 and l2: if l1.val <= l2.val: - cur.next = l1 + tail.next = l1 l1 = l1.next else: - cur.next = l2 + tail.next = l2 l2 = l2.next - cur = cur.next - cur.next = l1 or l2 + tail = tail.next + tail.next = l1 or l2 return dummy.next ``` @@ -120,23 +129,23 @@ class Solution { slow = slow.next; fast = fast.next.next; } - ListNode t = slow.next; + ListNode l1 = head, l2 = slow.next; slow.next = null; - ListNode l1 = sortList(head); - ListNode l2 = sortList(t); + l1 = sortList(l1); + l2 = sortList(l2); ListNode dummy = new ListNode(); - ListNode cur = dummy; + ListNode tail = dummy; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { - cur.next = l1; + tail.next = l1; l1 = l1.next; } else { - cur.next = l2; + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 == null ? l2 : l1; + tail.next = l1 != null ? l1 : l2; return dummy.next; } } @@ -158,30 +167,33 @@ class Solution { class Solution { public: ListNode* sortList(ListNode* head) { - if (!head || !head->next) return head; - auto* slow = head; - auto* fast = head->next; + if (!head || !head->next) { + return head; + } + ListNode* slow = head; + ListNode* fast = head->next; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } - auto* t = slow->next; + ListNode* l1 = head; + ListNode* l2 = slow->next; slow->next = nullptr; - auto* l1 = sortList(head); - auto* l2 = sortList(t); - auto* dummy = new ListNode(); - auto* cur = dummy; + l1 = sortList(l1); + l2 = sortList(l2); + ListNode* dummy = new ListNode(); + ListNode* tail = dummy; while (l1 && l2) { if (l1->val <= l2->val) { - cur->next = l1; + tail->next = l1; l1 = l1->next; } else { - cur->next = l2; + tail->next = l2; l2 = l2->next; } - cur = cur->next; + tail = tail->next; } - cur->next = l1 ? l1 : l2; + tail->next = l1 ? l1 : l2; return dummy->next; } }; @@ -205,25 +217,27 @@ func sortList(head *ListNode) *ListNode { for fast != nil && fast.Next != nil { slow, fast = slow.Next, fast.Next.Next } - t := slow.Next + l1 := head + l2 := slow.Next slow.Next = nil - l1, l2 := sortList(head), sortList(t) + l1 = sortList(l1) + l2 = sortList(l2) dummy := &ListNode{} - cur := dummy + tail := dummy for l1 != nil && l2 != nil { if l1.Val <= l2.Val { - cur.Next = l1 + tail.Next = l1 l1 = l1.Next } else { - cur.Next = l2 + tail.Next = l2 l2 = l2.Next } - cur = cur.Next + tail = tail.Next } if l1 != nil { - cur.Next = l1 + tail.Next = l1 } else { - cur.Next = l2 + tail.Next = l2 } return dummy.Next } @@ -245,32 +259,31 @@ func sortList(head *ListNode) *ListNode { */ function sortList(head: ListNode | null): ListNode | null { - if (head == null || head.next == null) return head; - // 快慢指针定位中点 - let slow: ListNode = head, - fast: ListNode = head.next; - while (fast != null && fast.next != null) { - slow = slow.next; + if (head === null || head.next === null) { + return head; + } + let [slow, fast] = [head, head.next]; + while (fast !== null && fast.next !== null) { + slow = slow.next!; fast = fast.next.next; } - // 归并排序 - let mid: ListNode = slow.next; + let [l1, l2] = [head, slow.next]; slow.next = null; - let l1: ListNode = sortList(head); - let l2: ListNode = sortList(mid); - let dummy: ListNode = new ListNode(); - let cur: ListNode = dummy; - while (l1 != null && l2 != null) { + l1 = sortList(l1); + l2 = sortList(l2); + const dummy = new ListNode(); + let tail = dummy; + while (l1 !== null && l2 !== null) { if (l1.val <= l2.val) { - cur.next = l1; + tail.next = l1; l1 = l1.next; } else { - cur.next = l2; + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 == null ? l2 : l1; + tail.next = l1 ?? l2; return dummy.next; } ``` @@ -351,32 +364,31 @@ impl Solution { * @return {ListNode} */ var sortList = function (head) { - if (!head || !head.next) { + if (head === null || head.next === null) { return head; } - let slow = head; - let fast = head.next; - while (fast && fast.next) { + let [slow, fast] = [head, head.next]; + while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; } - let t = slow.next; + let [l1, l2] = [head, slow.next]; slow.next = null; - let l1 = sortList(head); - let l2 = sortList(t); + l1 = sortList(l1); + l2 = sortList(l2); const dummy = new ListNode(); - let cur = dummy; - while (l1 && l2) { + let tail = dummy; + while (l1 !== null && l2 !== null) { if (l1.val <= l2.val) { - cur.next = l1; + tail.next = l1; l1 = l1.next; } else { - cur.next = l2; + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 || l2; + tail.next = l1 ?? l2; return dummy.next; }; ``` @@ -397,37 +409,31 @@ var sortList = function (head) { */ public class Solution { public ListNode SortList(ListNode head) { - if (head == null || head.next == null) - { + if (head == null || head.next == null) { return head; } ListNode slow = head, fast = head.next; - while (fast != null && fast.next != null) - { + while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } - ListNode t = slow.next; + ListNode l1 = head, l2 = slow.next; slow.next = null; - ListNode l1 = SortList(head); - ListNode l2 = SortList(t); + l1 = SortList(l1); + l2 = SortList(l2); ListNode dummy = new ListNode(); - ListNode cur = dummy; - while (l1 != null && l2 != null) - { - if (l1.val <= l2.val) - { - cur.next = l1; + ListNode tail = dummy; + while (l1 != null && l2 != null) { + if (l1.val <= l2.val) { + tail.next = l1; l1 = l1.next; - } - else - { - cur.next = l2; + } else { + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 == null ? l2 : l1; + tail.next = l1 != null ? l1 : l2; return dummy.next; } } diff --git a/solution/0100-0199/0148.Sort List/Solution.cpp b/solution/0100-0199/0148.Sort List/Solution.cpp index f41cd39e5ddb7..1128a8643cdf0 100644 --- a/solution/0100-0199/0148.Sort List/Solution.cpp +++ b/solution/0100-0199/0148.Sort List/Solution.cpp @@ -11,30 +11,33 @@ class Solution { public: ListNode* sortList(ListNode* head) { - if (!head || !head->next) return head; - auto* slow = head; - auto* fast = head->next; + if (!head || !head->next) { + return head; + } + ListNode* slow = head; + ListNode* fast = head->next; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } - auto* t = slow->next; + ListNode* l1 = head; + ListNode* l2 = slow->next; slow->next = nullptr; - auto* l1 = sortList(head); - auto* l2 = sortList(t); - auto* dummy = new ListNode(); - auto* cur = dummy; + l1 = sortList(l1); + l2 = sortList(l2); + ListNode* dummy = new ListNode(); + ListNode* tail = dummy; while (l1 && l2) { if (l1->val <= l2->val) { - cur->next = l1; + tail->next = l1; l1 = l1->next; } else { - cur->next = l2; + tail->next = l2; l2 = l2->next; } - cur = cur->next; + tail = tail->next; } - cur->next = l1 ? l1 : l2; + tail->next = l1 ? l1 : l2; return dummy->next; } -}; \ No newline at end of file +}; diff --git a/solution/0100-0199/0148.Sort List/Solution.cs b/solution/0100-0199/0148.Sort List/Solution.cs index fc6c6d8c95300..06cca43306687 100644 --- a/solution/0100-0199/0148.Sort List/Solution.cs +++ b/solution/0100-0199/0148.Sort List/Solution.cs @@ -11,37 +11,31 @@ */ public class Solution { public ListNode SortList(ListNode head) { - if (head == null || head.next == null) - { + if (head == null || head.next == null) { return head; } ListNode slow = head, fast = head.next; - while (fast != null && fast.next != null) - { + while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } - ListNode t = slow.next; + ListNode l1 = head, l2 = slow.next; slow.next = null; - ListNode l1 = SortList(head); - ListNode l2 = SortList(t); + l1 = SortList(l1); + l2 = SortList(l2); ListNode dummy = new ListNode(); - ListNode cur = dummy; - while (l1 != null && l2 != null) - { - if (l1.val <= l2.val) - { - cur.next = l1; + ListNode tail = dummy; + while (l1 != null && l2 != null) { + if (l1.val <= l2.val) { + tail.next = l1; l1 = l1.next; - } - else - { - cur.next = l2; + } else { + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 == null ? l2 : l1; + tail.next = l1 != null ? l1 : l2; return dummy.next; } } diff --git a/solution/0100-0199/0148.Sort List/Solution.go b/solution/0100-0199/0148.Sort List/Solution.go index 0e58da35af105..5727769dac5a1 100644 --- a/solution/0100-0199/0148.Sort List/Solution.go +++ b/solution/0100-0199/0148.Sort List/Solution.go @@ -13,25 +13,27 @@ func sortList(head *ListNode) *ListNode { for fast != nil && fast.Next != nil { slow, fast = slow.Next, fast.Next.Next } - t := slow.Next + l1 := head + l2 := slow.Next slow.Next = nil - l1, l2 := sortList(head), sortList(t) + l1 = sortList(l1) + l2 = sortList(l2) dummy := &ListNode{} - cur := dummy + tail := dummy for l1 != nil && l2 != nil { if l1.Val <= l2.Val { - cur.Next = l1 + tail.Next = l1 l1 = l1.Next } else { - cur.Next = l2 + tail.Next = l2 l2 = l2.Next } - cur = cur.Next + tail = tail.Next } if l1 != nil { - cur.Next = l1 + tail.Next = l1 } else { - cur.Next = l2 + tail.Next = l2 } return dummy.Next -} \ No newline at end of file +} diff --git a/solution/0100-0199/0148.Sort List/Solution.java b/solution/0100-0199/0148.Sort List/Solution.java index accbe7a7412bf..90709228bdaef 100644 --- a/solution/0100-0199/0148.Sort List/Solution.java +++ b/solution/0100-0199/0148.Sort List/Solution.java @@ -18,23 +18,23 @@ public ListNode sortList(ListNode head) { slow = slow.next; fast = fast.next.next; } - ListNode t = slow.next; + ListNode l1 = head, l2 = slow.next; slow.next = null; - ListNode l1 = sortList(head); - ListNode l2 = sortList(t); + l1 = sortList(l1); + l2 = sortList(l2); ListNode dummy = new ListNode(); - ListNode cur = dummy; + ListNode tail = dummy; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { - cur.next = l1; + tail.next = l1; l1 = l1.next; } else { - cur.next = l2; + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 == null ? l2 : l1; + tail.next = l1 != null ? l1 : l2; return dummy.next; } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0148.Sort List/Solution.js b/solution/0100-0199/0148.Sort List/Solution.js index 0600b341e2bc0..e27d571b920c8 100644 --- a/solution/0100-0199/0148.Sort List/Solution.js +++ b/solution/0100-0199/0148.Sort List/Solution.js @@ -10,31 +10,30 @@ * @return {ListNode} */ var sortList = function (head) { - if (!head || !head.next) { + if (head === null || head.next === null) { return head; } - let slow = head; - let fast = head.next; - while (fast && fast.next) { + let [slow, fast] = [head, head.next]; + while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; } - let t = slow.next; + let [l1, l2] = [head, slow.next]; slow.next = null; - let l1 = sortList(head); - let l2 = sortList(t); + l1 = sortList(l1); + l2 = sortList(l2); const dummy = new ListNode(); - let cur = dummy; - while (l1 && l2) { + let tail = dummy; + while (l1 !== null && l2 !== null) { if (l1.val <= l2.val) { - cur.next = l1; + tail.next = l1; l1 = l1.next; } else { - cur.next = l2; + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 || l2; + tail.next = l1 ?? l2; return dummy.next; }; diff --git a/solution/0100-0199/0148.Sort List/Solution.py b/solution/0100-0199/0148.Sort List/Solution.py index ce9ef7b6ae054..2e16d09f70355 100644 --- a/solution/0100-0199/0148.Sort List/Solution.py +++ b/solution/0100-0199/0148.Sort List/Solution.py @@ -4,24 +4,25 @@ # self.val = val # self.next = next class Solution: - def sortList(self, head: ListNode) -> ListNode: + def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: if head is None or head.next is None: return head slow, fast = head, head.next while fast and fast.next: - slow, fast = slow.next, fast.next.next - t = slow.next + slow = slow.next + fast = fast.next.next + l1, l2 = head, slow.next slow.next = None - l1, l2 = self.sortList(head), self.sortList(t) + l1, l2 = self.sortList(l1), self.sortList(l2) dummy = ListNode() - cur = dummy + tail = dummy while l1 and l2: if l1.val <= l2.val: - cur.next = l1 + tail.next = l1 l1 = l1.next else: - cur.next = l2 + tail.next = l2 l2 = l2.next - cur = cur.next - cur.next = l1 or l2 + tail = tail.next + tail.next = l1 or l2 return dummy.next diff --git a/solution/0100-0199/0148.Sort List/Solution.ts b/solution/0100-0199/0148.Sort List/Solution.ts index 532a27a51a31e..08b2d7542a1e2 100644 --- a/solution/0100-0199/0148.Sort List/Solution.ts +++ b/solution/0100-0199/0148.Sort List/Solution.ts @@ -11,31 +11,30 @@ */ function sortList(head: ListNode | null): ListNode | null { - if (head == null || head.next == null) return head; - // 快慢指针定位中点 - let slow: ListNode = head, - fast: ListNode = head.next; - while (fast != null && fast.next != null) { - slow = slow.next; + if (head === null || head.next === null) { + return head; + } + let [slow, fast] = [head, head.next]; + while (fast !== null && fast.next !== null) { + slow = slow.next!; fast = fast.next.next; } - // 归并排序 - let mid: ListNode = slow.next; + let [l1, l2] = [head, slow.next]; slow.next = null; - let l1: ListNode = sortList(head); - let l2: ListNode = sortList(mid); - let dummy: ListNode = new ListNode(); - let cur: ListNode = dummy; - while (l1 != null && l2 != null) { + l1 = sortList(l1); + l2 = sortList(l2); + const dummy = new ListNode(); + let tail = dummy; + while (l1 !== null && l2 !== null) { if (l1.val <= l2.val) { - cur.next = l1; + tail.next = l1; l1 = l1.next; } else { - cur.next = l2; + tail.next = l2; l2 = l2.next; } - cur = cur.next; + tail = tail.next; } - cur.next = l1 == null ? l2 : l1; + tail.next = l1 ?? l2; return dummy.next; }