Merge In Between Linked Lists LeetCode solution in C++

1
class Solution {
2
public:
3
ListNode *mergeInBetween(ListNode *list1, int a, int b, ListNode *list2) {
4
int jump1 = 1;
5
ListNode *temp1 = list1;
6
while (jump1 < a) {
7
temp1 = temp1->next;
8
jump1++;
9
} // Gets the pointer to a
10

11
int jump2 = 1;
12
ListNode *temp2 = list1;
13
while (jump2 <= b) {
14
temp2 = temp2->next;
15
jump2++;
16
} // Gets the pointer to b
17

18
ListNode *temp3 = list2;
19
while (temp3->next != NULL) {
20
temp3 = temp3->next;
21
} // Gets the pointer to the tail of list2
22

23
temp1->next = list2; // set the next pointer of a to the head of list2
24

25
temp3->next = temp2->next; // set next of tail of list2 to the pointer to b
26

27
return list1; // return the original list i.e. list1
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0