Skip to content

Commit 057d4aa

Browse files
committed
Simplify sort-list.cc
1 parent 98c50f7 commit 057d4aa

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

sort-list.cc

+6-14
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
class Solution {
66
public:
77
ListNode *sortList(ListNode *head) {
8-
if (! head)
9-
return head;
108
for(;;) {
11-
ListNode *p = head, *q, *r, *s, *head2 = NULL, *l = NULL;
9+
ListNode *p = head, *q, *r, *s, *head2 = NULL, **l = &head2;
1210
bool ok = true;
1311
while (p) {
1412
q = p;
@@ -25,23 +23,17 @@ class Solution {
2523
ok = false;
2624
while (p != q || r != s)
2725
if (r == s || p != q && p->val < r->val) {
28-
if (l)
29-
l->next = p;
30-
else
31-
head2 = p;
32-
l = p;
26+
*l = p;
27+
l = &p->next;
3328
p = p->next;
3429
} else {
35-
if (l)
36-
l->next = r;
37-
else
38-
head2 = r;
39-
l = r;
30+
*l = r;
31+
l = &r->next;
4032
r = r->next;
4133
}
4234
p = s;
4335
}
44-
l->next = NULL;
36+
*l = NULL;
4537
head = head2;
4638
if (ok) break;
4739
}

0 commit comments

Comments
 (0)