Skip to content

Commit d4fd234

Browse files
Merge pull request pranjay-poddar#180 from cintamg/master
Merge In Between Linked Lists
2 parents af63bc1 + f91540b commit d4fd234

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

c/Merge_between_list.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* You are given two linked lists list1, list2 of size n amd m respectively.
2+
Remove ath to bth node of list1 and insert list2 in their place.
3+
*/
4+
5+
/* Example 1:
6+
Input : list1 = [0, 1, 2, 3, 4, 5], a=3, b=4, list2 = [10, 20, 30]
7+
Output : [0, 1, 2, 10, 20, 30, 5]
8+
*/
9+
10+
/* Example 2:
11+
Input : list1 = [1, 2, 3, 4, 5, 6], a=2, b=4, list2 = [100, 200, 300]
12+
Output : [1, 2, 100, 200, 300, 6]
13+
*/
14+
15+
/*
16+
Definition for singly-linked list.
17+
struct ListNode {
18+
int val;
19+
struct ListNode *next;
20+
};
21+
*/
22+
23+
24+
struct ListNode* mergeInBetween(struct ListNode* list1, int a, int b, struct ListNode* list2){
25+
struct ListNode *temp=list1,*temp1,*temp2;
26+
int i;
27+
for(i=0;i<a-1;i++)
28+
temp=temp->next;
29+
temp1=temp;
30+
for(;i<=b;i++)
31+
temp=temp->next;
32+
temp2=temp;
33+
temp1->next=list2;
34+
while(list2->next!=NULL)
35+
list2=list2->next;
36+
list2->next=temp2;
37+
return list1;
38+
}

0 commit comments

Comments
 (0)