🌐 AI搜索 & 代理 主页
Skip to content

Commit e77cf36

Browse files
authored
Added tasks 21-35
1 parent 98d32cf commit e77cf36

File tree

21 files changed

+888
-0
lines changed

21 files changed

+888
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
6161

6262
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
6363
|-|-|-|-|-|-
64+
| 0021 |[Merge Two Sorted Lists](src/main/c/g0001_0100/s0021_merge_two_sorted_lists/Solution.c)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
6465

6566
#### Day 8 Linked List
6667

@@ -162,11 +163,13 @@ C-based LeetCode algorithm problem solutions, regularly updated.
162163

163164
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
164165
|-|-|-|-|-|-
166+
| 0024 |[Swap Nodes in Pairs](src/main/c/g0001_0100/s0024_swap_nodes_in_pairs/Solution.c)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
165167

166168
#### Day 13 Linked List
167169

168170
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
169171
|-|-|-|-|-|-
172+
| 0025 |[Reverse Nodes in k-Group](src/main/c/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.c)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
170173

171174
#### Day 14 Stack Queue
172175

@@ -214,6 +217,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
214217

215218
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
216219
|-|-|-|-|-|-
220+
| 0035 |[Search Insert Position](src/main/c/g0001_0100/s0035_search_insert_position/Solution.c)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
217221

218222
#### Day 2 Two Pointers
219223

@@ -261,6 +265,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
261265

262266
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
263267
|-|-|-|-|-|-
268+
| 0021 |[Merge Two Sorted Lists](src/main/c/g0001_0100/s0021_merge_two_sorted_lists/Solution.c)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
264269

265270
#### Day 11 Recursion Backtracking
266271

@@ -288,6 +293,8 @@ C-based LeetCode algorithm problem solutions, regularly updated.
288293

289294
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
290295
|-|-|-|-|-|-
296+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/c/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
297+
| 0033 |[Search in Rotated Sorted Array](src/main/c/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
291298

292299
#### Day 2 Binary Search
293300

@@ -341,6 +348,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
341348
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
342349
|-|-|-|-|-|-
343350
| 0017 |[Letter Combinations of a Phone Number](src/main/c/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 0 | 100.00
351+
| 0022 |[Generate Parentheses](src/main/c/g0001_0100/s0022_generate_parentheses/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
344352

345353
#### Day 12 Dynamic Programming
346354

@@ -404,6 +412,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
404412

405413
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
406414
|-|-|-|-|-|-
415+
| 0035 |[Search Insert Position](src/main/c/g0001_0100/s0035_search_insert_position/Solution.c)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
407416

408417
#### Day 3
409418

@@ -419,6 +428,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
419428

420429
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
421430
|-|-|-|-|-|-
431+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/c/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
422432

423433
#### Day 6
424434

@@ -449,6 +459,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
449459

450460
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
451461
|-|-|-|-|-|-
462+
| 0033 |[Search in Rotated Sorted Array](src/main/c/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
452463

453464
#### Day 12
454465

@@ -970,6 +981,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
970981

971982
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
972983
|-|-|-|-|-|-
984+
| 0021 |[Merge Two Sorted Lists](src/main/c/g0001_0100/s0021_merge_two_sorted_lists/Solution.c)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
973985

974986
#### Day 4 Linked List
975987

@@ -1074,6 +1086,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
10741086

10751087
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10761088
|-|-|-|-|-|-
1089+
| 0033 |[Search in Rotated Sorted Array](src/main/c/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
10771090

10781091
#### Day 9 Binary Search Tree
10791092

@@ -1157,6 +1170,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
11571170

11581171
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11591172
|-|-|-|-|-|-
1173+
| 0033 |[Search in Rotated Sorted Array](src/main/c/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
11601174

11611175
#### Udemy Arrays
11621176

@@ -1189,6 +1203,9 @@ C-based LeetCode algorithm problem solutions, regularly updated.
11891203

11901204
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11911205
|-|-|-|-|-|-
1206+
| 0024 |[Swap Nodes in Pairs](src/main/c/g0001_0100/s0024_swap_nodes_in_pairs/Solution.c)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1207+
| 0021 |[Merge Two Sorted Lists](src/main/c/g0001_0100/s0021_merge_two_sorted_lists/Solution.c)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
1208+
| 0025 |[Reverse Nodes in k-Group](src/main/c/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.c)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
11921209

11931210
#### Udemy Tree Stack Queue
11941211

@@ -1215,6 +1232,7 @@ C-based LeetCode algorithm problem solutions, regularly updated.
12151232

12161233
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12171234
|-|-|-|-|-|-
1235+
| 0022 |[Generate Parentheses](src/main/c/g0001_0100/s0022_generate_parentheses/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
12181236
| 0017 |[Letter Combinations of a Phone Number](src/main/c/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 0 | 100.00
12191237

12201238
#### Udemy Bit Manipulation
@@ -1231,6 +1249,16 @@ C-based LeetCode algorithm problem solutions, regularly updated.
12311249

12321250
| # | Title | Difficulty | Tag | Time, ms | Time, %
12331251
|------|----------------|-------------|-------------|----------|---------
1252+
| 0035 |[Search Insert Position](src/main/c/g0001_0100/s0035_search_insert_position/Solution.c)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_2, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
1253+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/c/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_5, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
1254+
| 0033 |[Search in Rotated Sorted Array](src/main/c/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_11, Level_2_Day_8_Binary_Search, Udemy_Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
1255+
| 0032 |[Longest Valid Parentheses](src/main/c/g0001_0100/s0032_longest_valid_parentheses/Solution.c)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1256+
| 0031 |[Next Permutation](src/main/c/g0001_0100/s0031_next_permutation/Solution.c)| Medium | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1257+
| 0025 |[Reverse Nodes in k-Group](src/main/c/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.c)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_13_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
1258+
| 0024 |[Swap Nodes in Pairs](src/main/c/g0001_0100/s0024_swap_nodes_in_pairs/Solution.c)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_12_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1259+
| 0023 |[Merge k Sorted Lists](src/main/c/g0001_0100/s0023_merge_k_sorted_lists/Solution.c)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Heap_Priority_Queue, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(k\*n\*log(k))_Space_O(log(k)) | 0 | 100.00
1260+
| 0022 |[Generate Parentheses](src/main/c/g0001_0100/s0022_generate_parentheses/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
1261+
| 0021 |[Merge Two Sorted Lists](src/main/c/g0001_0100/s0021_merge_two_sorted_lists/Solution.c)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_7_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
12341262
| 0020 |[Valid Parentheses](src/main/c/g0001_0100/s0020_valid_parentheses/Solution.c)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Data_Structure_I_Day_9_Stack_Queue, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
12351263
| 0019 |[Remove Nth Node From End of List](src/main/c/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Algorithm_I_Day_5_Two_Pointers, Level_2_Day_3_Linked_List, Big_O_Time_O(L)_Space_O(L) | 0 | 100.00
12361264
| 0017 |[Letter Combinations of a Phone Number](src/main/c/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.c)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(4^n)_Space_O(n) | 0 | 100.00
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Linked_List #Recursion
2+
// #Data_Structure_I_Day_7_Linked_List #Algorithm_I_Day_10_Recursion_Backtracking
3+
// #Level_1_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(m+n)_Space_O(m+n)
4+
// #2024_10_29_Time_0_ms_(100.00%)_Space_10.7_MB_(26.00%)
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* struct ListNode {
9+
* int val;
10+
* struct ListNode *next;
11+
* };
12+
*/
13+
// Function to create a new ListNode
14+
struct ListNode* createNode(int val) {
15+
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
16+
node->val = val;
17+
node->next = NULL;
18+
return node;
19+
}
20+
21+
// Function to merge two sorted linked lists
22+
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
23+
struct ListNode dummy = {-1, NULL}; // Dummy head node
24+
struct ListNode* list = &dummy; // Pointer to the dummy node
25+
26+
while (l1 != NULL || l2 != NULL) {
27+
if (l1 != NULL && l2 != NULL) {
28+
if (l1->val <= l2->val) {
29+
list->next = createNode(l1->val);
30+
l1 = l1->next;
31+
} else {
32+
list->next = createNode(l2->val);
33+
l2 = l2->next;
34+
}
35+
} else if (l1 != NULL) {
36+
list->next = createNode(l1->val);
37+
l1 = l1->next;
38+
} else {
39+
list->next = createNode(l2->val);
40+
l2 = l2->next;
41+
}
42+
list = list->next;
43+
}
44+
45+
return dummy.next; // Return the merged list starting from dummy's next node
46+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
21\. Merge Two Sorted Lists
2+
3+
Easy
4+
5+
Merge two sorted linked lists and return it as a **sorted** list. The list should be made by splicing together the nodes of the first two lists.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg)
10+
11+
**Input:** l1 = [1,2,4], l2 = [1,3,4]
12+
13+
**Output:** [1,1,2,3,4,4]
14+
15+
**Example 2:**
16+
17+
**Input:** l1 = [], l2 = []
18+
19+
**Output:** []
20+
21+
**Example 3:**
22+
23+
**Input:** l1 = [], l2 = [0]
24+
25+
**Output:** [0]
26+
27+
**Constraints:**
28+
29+
* The number of nodes in both lists is in the range `[0, 50]`.
30+
* `-100 <= Node.val <= 100`
31+
* Both `l1` and `l2` are sorted in **non-decreasing** order.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
2+
// #Backtracking #Algorithm_II_Day_11_Recursion_Backtracking #Udemy_Backtracking/Recursion
3+
// #Big_O_Time_O(2^n)_Space_O(n) #2024_10_29_Time_0_ms_(100.00%)_Space_9.2_MB_(78.89%)
4+
5+
/**
6+
* Note: The returned array must be malloced, assume caller calls free().
7+
*/
8+
int countPossibilities(int open, int closed, int n) {
9+
if (closed == n) {
10+
return 1;
11+
}
12+
13+
if (open == n) {
14+
return countPossibilities(open, closed + 1, n);
15+
}
16+
17+
if (open == closed) {
18+
return countPossibilities(open + 1, closed, n);
19+
}
20+
21+
return countPossibilities(open + 1, closed, n) + countPossibilities(open, closed + 1, n);
22+
}
23+
24+
void generateParentasis(
25+
int open,
26+
int closed,
27+
int n,
28+
char* currentString,
29+
int currentIndex,
30+
char** result,
31+
int *resultIndex) {
32+
33+
if (closed == n) {
34+
result[*resultIndex] = currentString;
35+
(*resultIndex)++;
36+
return;
37+
}
38+
39+
if (open == closed) {
40+
currentString[currentIndex] = '(';
41+
generateParentasis(open + 1, closed, n, currentString, currentIndex + 1, result, resultIndex);
42+
return;
43+
}
44+
45+
if (open == n) {
46+
currentString[currentIndex] = ')';
47+
generateParentasis(open, closed + 1, n, currentString, currentIndex + 1, result, resultIndex);
48+
return;
49+
}
50+
51+
char* duplicatedString = malloc(sizeof(char) * (n * 2 + 1));
52+
strcpy(duplicatedString, currentString);
53+
currentString[currentIndex] = '(';
54+
generateParentasis(open + 1, closed, n, currentString, currentIndex + 1, result, resultIndex);
55+
duplicatedString[currentIndex] = ')';
56+
generateParentasis(open, closed + 1, n, duplicatedString, currentIndex + 1, result, resultIndex);
57+
}
58+
59+
char** generateParenthesis(int n, int* returnSize) {
60+
*returnSize = countPossibilities(0, 0, n);
61+
char** result = malloc(sizeof(char*) * (*returnSize));
62+
int resultIndex = 0;
63+
char * currentString = malloc(sizeof(char) * (n * 2 + 1));
64+
currentString[n * 2] = '\0';
65+
generateParentasis(0, 0, n, currentString, 0, result, &resultIndex);
66+
return result;
67+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
22\. Generate Parentheses
2+
3+
Medium
4+
5+
Given `n` pairs of parentheses, write a function to _generate all combinations of well-formed parentheses_.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 3
10+
11+
**Output:** ["((()))","(()())","(())()","()(())","()()()"]
12+
13+
**Example 2:**
14+
15+
**Input:** n = 1
16+
17+
**Output:** ["()"]
18+
19+
**Constraints:**
20+
21+
* `1 <= n <= 8`
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Heap_Priority_Queue #Linked_List
2+
// #Divide_and_Conquer #Merge_Sort #Big_O_Time_O(k*n*log(k))_Space_O(log(k))
3+
// #2024_10_29_Time_0_ms_(100.00%)_Space_13.8_MB_(64.94%)
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
/**
9+
* Definition for singly-linked list.
10+
* struct ListNode {
11+
* int val;
12+
* struct ListNode *next;
13+
* };
14+
*/
15+
// Function to create a new ListNode
16+
struct ListNode* createNode(int val) {
17+
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
18+
node->val = val;
19+
node->next = NULL;
20+
return node;
21+
}
22+
23+
// Function to merge two sorted linked lists
24+
struct ListNode* mergeTwoLists(struct ListNode* left, struct ListNode* right) {
25+
if (left == NULL) return right;
26+
if (right == NULL) return left;
27+
28+
struct ListNode* res;
29+
if (left->val <= right->val) {
30+
res = left;
31+
left = left->next;
32+
} else {
33+
res = right;
34+
right = right->next;
35+
}
36+
37+
struct ListNode* node = res;
38+
while (left != NULL || right != NULL) {
39+
if (left == NULL) {
40+
node->next = right;
41+
right = right->next;
42+
} else if (right == NULL) {
43+
node->next = left;
44+
left = left->next;
45+
} else {
46+
if (left->val <= right->val) {
47+
node->next = left;
48+
left = left->next;
49+
} else {
50+
node->next = right;
51+
right = right->next;
52+
}
53+
}
54+
node = node->next;
55+
}
56+
return res;
57+
}
58+
59+
// Recursive function to merge k sorted lists
60+
struct ListNode* mergeKListsHelper(struct ListNode** lists, int leftIndex, int rightIndex) {
61+
if (rightIndex > leftIndex + 1) {
62+
int mid = (leftIndex + rightIndex) / 2;
63+
struct ListNode* left = mergeKListsHelper(lists, leftIndex, mid);
64+
struct ListNode* right = mergeKListsHelper(lists, mid, rightIndex);
65+
return mergeTwoLists(left, right);
66+
} else {
67+
return lists[leftIndex];
68+
}
69+
}
70+
71+
// Function to initiate mergeKLists with bounds
72+
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) {
73+
if (listsSize == 0) return NULL;
74+
return mergeKListsHelper(lists, 0, listsSize);
75+
}

0 commit comments

Comments
 (0)