-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path1669.java
More file actions
36 lines (33 loc) · 956 Bytes
/
1669.java
File metadata and controls
36 lines (33 loc) · 956 Bytes
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
ListNode current = list1;
int i = 0, j = 0;
ListNode last2 = list2;
while(last2.next != null){
last2 = last2.next;
}
while(i <a-1 && current != null){
current = current.next;
i++;
}
ListNode current2 = list1;
while(j <b && current2 != null){
current2 = current2.next;
j++;
}
last2.next = current2.next;
current2.next = null;
current.next = list2;
return list1;
}
}