nikunjsoni

Untitled

Mar 16th, 2021
57
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  *     int val;
  5.  *     ListNode *next;
  6.  *     ListNode() : val(0), next(nullptr) {}
  7.  *     ListNode(int x) : val(x), next(nullptr) {}
  8.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11. class Solution {
  12. public:
  13.     ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
  14.         ListNode *start, *end;
  15.         start = list1; end = list1;
  16.         a--;
  17.         while(a || b){
  18.             if(a) start = start->next, a--;
  19.             if(b) end = end->next, b--;
  20.         }
  21.         start->next = list2;
  22.         while(list2->next != nullptr){
  23.             list2 = list2->next;
  24.         }
  25.         list2->next = end->next;
  26.         return list1;
  27.     }
  28. };
RAW Paste Data