Skip to content

Commit 9159710

Browse files
author
Victor
authored
simplified
1 parent 7f2f5a2 commit 9159710

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

23. Merge k Sorted Lists.c

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,61 @@ Merge k sorted linked lists and return it as one sorted list. Analyze and descri
77
/**
88
* Definition for singly-linked list.
99
* struct ListNode {
10-
*     int val;
11-
*     struct ListNode *next;
10+
* int val;
11+
* struct ListNode *next;
1212
* };
1313
*/
1414
struct ListNode *merge(struct ListNode *a, struct ListNode *b) {
15-
   struct ListNode *head, *prev, *c;
16-
   head = prev = NULL;
17-
   while (a && b) {
18-
       if (a->val < b->val) {
19-
           c = a;
20-
           a = a->next;
21-
      } else {
22-
           c = b;
23-
           b = b->next;
24-
      }
25-
       if (!head) head = c;
26-
       if (prev) prev->next = c;
27-
       prev = c;
28-
  }
29-
   if (!head) head = a ? a : b;
30-
   if (prev) prev->next = a ? a : b;
31-
   return head;
15+
struct ListNode head, *prev, *c;
16+
prev = &head;
17+
while (a && b) {
18+
if (a->val < b->val) {
19+
c = a;
20+
a = a->next;
21+
} else {
22+
c = b;
23+
b = b->next;
24+
}
25+
prev->next = c;
26+
prev = c;
27+
}
28+
prev->next = a ? a : b;
29+
return head.next;
3230
}
3331
struct ListNode *divide_and_merge(struct ListNode **lists, int start, int end) {
34-
   int mid;
35-
   struct ListNode *a, *b;
36-
   
37-
   if (start == end) return lists[start];
38-
   
39-
   mid = start + (end - start) / 2;
40-
   
41-
   a = divide_and_merge(lists, start, mid);
42-
   b = divide_and_merge(lists, mid + 1, end);
43-
   
44-
   return merge(a, b);
32+
int mid;
33+
struct ListNode *a, *b;
34+
35+
if (start == end) return lists[start];
36+
37+
mid = start + (end - start) / 2;
38+
39+
a = divide_and_merge(lists, start, mid);
40+
b = divide_and_merge(lists, mid + 1, end);
41+
42+
return merge(a, b);
4543
}
4644
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) {
47-
   struct ListNode *head = NULL;
48-
49-
   if (listsSize == 0) return NULL;
50-
51-
#if 0  // 245ms
52-
   int i;
53-
   struct ListNode *a, *b;
54-
   
55-
   a = lists[0];
56-
   for (i = 1; i < listsSize; i ++) {
57-
       b = lists[i];
58-
       a = merge(a, b);
59-
  }
60-
   head = a;
61-
#else  // 9ms   O(NKlogK) N is average length of a list, K is number of lists
62-
   head = divide_and_merge(lists, 0, listsSize - 1);
45+
struct ListNode *head = NULL;
46+
47+
if (listsSize == 0) return NULL;
48+
49+
#if 0 // 245ms
50+
int i;
51+
struct ListNode *a, *b;
52+
53+
a = lists[0];
54+
for (i = 1; i < listsSize; i ++) {
55+
b = lists[i];
56+
a = merge(a, b);
57+
}
58+
head = a;
59+
#else // 9ms O(NKlogK) N is average length of a list, K is number of lists
60+
head = divide_and_merge(lists, 0, listsSize - 1);
6361
#endif
64-
   return head;
62+
return head;
6563
}
6664

67-
6865
/*
6966
Difficulty:Hard
7067
Total Accepted:159.5K

0 commit comments

Comments
 (0)