You are given two linked lists: list1
and list2
of sizes n
and m
respectively.
Remove list1
's nodes from the ath
node to the bth
node, and put list2
in their place.
Example:
Input: list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
Output: [0,1,2,1000000,1000001,1000002,5]
Explanation: We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.
Approach:
C++
#include <bits/stdc++.h>using namespace std;//Struture of nodestruct ListNode{int data;ListNode *next;ListNode(int data){this->data = data;this->next = NULL;}};ListNode *mergeInBetween(ListNode *list1, int a, int b,ListNode *list2){ListNode *prev = NULL;ListNode *temp = list1;int i = 0;while (i < a){prev = temp;temp = temp->next;i++;}prev->next = list2;while (prev->next != NULL){prev = prev->next;}while (i < b){temp = temp->next;i++;}prev->next = temp->next;return list1;}//function to traverse a linked//listvoid linkedListTraversal(ListNode *head){ListNode *temp = head;//iterate till we reach end of the linked//listwhile (temp != NULL){//print the current//node datacout << temp->data;//move to next nodetemp = temp->next;if (temp != NULL)cout << ", ";}}int main(){ListNode *list1 = new ListNode(0);list1->next = new ListNode(1);list1->next->next = new ListNode(2);list1->next->next->next = new ListNode(3);list1->next->next->next->next = new ListNode(4);list1->next->next->next->next->next = new ListNode(5);int a = 3, b = 4;ListNode *list2 = new ListNode(1000000);list2->next = new ListNode(1000001);list2->next->next = new ListNode(1000002);ListNode *merge = mergeInBetween(list1, a, b, list2);cout << "[";linkedListTraversal(merge);cout << "]";return 0;}
No comments:
Post a Comment