Solved! Leetcode 1669. Merge In Between Linked Lists

Merge In Between Linked Lists

Description

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.

The blue edges and nodes in the following figure indicate the result:

Build the result list and return its head.

Example 1

Example 2

Constraints

  • 3 <= list1.length <= 104
  • 1 <= a <= b < list1.length - 1
  • 1 <= list2.length <= 104

Approach

  1. Get tail of list 2
  2. Get node a-1 and b+1
  3. Merge two lists by pointing list1 a-1.next to the head of list 2 and pointing the tail.next of list 2 to b+1 node of list 1

Solution

Rate this post

Leave a Reply