We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2fce508 commit 40fd08cCopy full SHA for 40fd08c
merge-two-sorted-lists/PDKhan.cpp
@@ -0,0 +1,37 @@
1
+class Solution {
2
+ public:
3
+ ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
4
+ ListNode* new_head = NULL;
5
+ ListNode* tail;
6
+
7
+ while(list1 || list2){
8
+ ListNode* curr;
9
10
+ if(list1 && list2){
11
+ if(list1->val < list2->val){
12
+ curr = list1;
13
+ list1 = list1->next;
14
+ }else{
15
+ curr = list2;
16
+ list2 = list2->next;
17
+ }
18
+ }else if(list1){
19
20
21
22
23
24
25
26
+ if(new_head == NULL){
27
+ new_head = curr;
28
+ tail = new_head;
29
30
+ tail->next = curr;
31
+ tail = tail->next;
32
33
34
35
+ return new_head;
36
37
+ };
0 commit comments