-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1669. Merge In Between Linked Lists
50 lines (36 loc) · 1.12 KB
/
1669. Merge In Between Linked Lists
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
40
41
42
43
44
45
46
47
48
49
50
/*
1669. Merge In Between Linked Lists
Runtime: 1 ms, faster than 100.00% of Java online submissions for Merge In Between Linked Lists.
Memory Usage: 42.5 MB, less than 95.86% of Java online submissions for Merge In Between Linked Lists.
*
* 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)
{
int first =0;
ListNode pointer1=list1;
ListNode pointer2=list1;
while(first<=b)
{ pointer1=pointer1.next;
first++;
}
first=0;
while(first<a-1)
{ pointer2=pointer2.next;
first++;
}
pointer2.next=list2;
while(list2.next!=null)
list2=list2.next;
list2.next=pointer1;
return list1;
}
}