Skip to content

Commit

Permalink
Create MergeInBetweenLinkedList.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored Mar 20, 2024
1 parent 056467f commit 75bfb25
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Linked Lists/MergeInBetweenLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
ListNode*start=NULL;
ListNode*end=list1;
for(int i=0;i<b;i++){
if(i==a-1){
start=end;
}
end=end->next;
}
start->next=list2;
ListNode*tail=list2;
while(tail->next){
tail=tail->next;

}
tail->next=end->next;
end->next=NULL;

return list1;
}
};

0 comments on commit 75bfb25

Please sign in to comment.