Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Coding: Linked List Segment Replacement

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Coding: Linked List Segment Replacement. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A JPMorganChase payment workflow stores ordered processing steps in a singly linked list. Given the heads of two singly linked lists and integers i and j, replace every node in the first list from index i through index j, inclusive, with the entire second list. Return the head of the updated list.

The input lists are represented by ListNode objects with integer val and next fields. The replacement must reuse the existing nodes rather than copying values into a new array.

Formal Specification

Implement splice_lists(head1, head2, i, j).

  • head1: head of a non-empty singly linked list
  • head2: head of a non-empty singly linked list
  • i, j: integers satisfying 0 <= i <= j < length(head1)
  • Return the head of the modified first list

Constraints

  • 1 <= length(head1) <= 10^4
  • 1 <= length(head2) <= 10^4
  • 0 <= i <= j < length(head1)
  • Node values are integers between -10^9 and 10^9
  • The lists do not share nodes before the operation

Function Signature

def splice_lists(head1, head2, i, j):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output