-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1669MergeInBetweenLinkedList.java
More file actions
39 lines (31 loc) · 1.25 KB
/
Q1669MergeInBetweenLinkedList.java
File metadata and controls
39 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
b-knd (jingru) on 01 August 2022 on 15:33:00
*/
class Solution {
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
//initiate dummyhead, set curr to be list1;
ListNode dummyHead = new ListNode();
ListNode curr = list1;
dummyHead.next = curr;
//keep on traversing curr until reaching index a
for(int i = 1; i < a; i++){
curr = curr.next;
}
//need to find the remaining of list 1 to be added to result
//use for loop to find first of the remaining list
ListNode temp = curr.next;
for(int i = 0; i <= b-a; i++){
temp = temp.next;
}
//add list2 to the linked list
curr.next = list2;
//traverse list2 until finding the tail node of list2, set its next pointer to be the head of the remaining list1 (temp)
while(curr.next != null){
curr = curr.next;
}
curr.next = temp;
return dummyHead.next;
}
}
//Runtime: 2 ms, faster than 87.18% of Java online submissions for Merge In Between Linked Lists.
//Memory Usage: 107.9 MB, less than 31.86% of Java online submissions for Merge In Between Linked Lists.