Leetcode Solution: #1669 Merge In Between Linked Lists πŸš€

leetcode-solution:-#1669-merge-in-between-linked-lists-

Question Type: Medium 🎚️
Complexities: Time: O(n), Space: O(1) 🚩

Code: πŸ‘‡

class Solution {
 public:
  ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
    ListNode* nodeBeforeA = list1;
    for (int i = 0; i < a - 1; ++i)
      nodeBeforeA = nodeBeforeA->next;

    ListNode* nodeB = nodeBeforeA->next;
    for (int i = 0; i < b - a; ++i)
      nodeB = nodeB->next;

    nodeBeforeA->next = list2;
    ListNode* lastNodeInList2 = list2;

    while (lastNodeInList2->next != nullptr)
      lastNodeInList2 = lastNodeInList2->next;

    lastNodeInList2->next = nodeB->next;
    nodeB->next = nullptr;
    return list1;
  }
};

Follow roshan_earth286 for More! ✌️

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
the-future-of-product-in-the-age-of-ai:-yana-welinder-(ceo,-kraftful)

The future of product in the age of AI: Yana Welinder (CEO, Kraftful)

Next Post
the-standard-explained:-what-is-iso-17025:-2017?

The Standard Explained: What is ISO 17025: 2017?

Related Posts