-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_In_Between_Linked_Lists.java
More file actions
38 lines (36 loc) · 1.03 KB
/
Merge_In_Between_Linked_Lists.java
File metadata and controls
38 lines (36 loc) · 1.03 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
import java.util.*;
import java.io.*;
import java.lang.*;
public class 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) {
ListNode start = list1;
for(int i = 0 ; i<a-1 ; i++)
{
start = start.next;
}
ListNode junc1 = start;
for(int i = 0 ; i<b-a+2 ; i++)
{
start = start.next;
}
ListNode junc2 = start;
ListNode start1 = list2;
while(start1.next!=null)
{
start1 = start1.next;
}
junc1.next = list2;
start1.next = junc2;
return list1;
}
}
}