Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Split Linked List by Position
00:00
5 left

Split Linked List by Position

EasyPython

Problem

Netskope SSE pipelines may process events as a singly linked list. Given the head of such a list, split it into two lists based on the nodes' 1-based positions: the first output contains positions 1, 3, 5, and so on, while the second contains positions 2, 4, 6, and so on.

Rewire the existing nodes instead of creating replacement nodes. Return the two head nodes as (odd_head, even_head). The relative order of nodes in each output list must be preserved.

A ListNode is provided with fields val and next. For test cases, linked lists are represented as Python arrays, and outputs are represented as a pair of arrays.

Constraints

  • 0 <= n <= 10^5
  • -10^9 <= node.val <= 10^9
  • The input is a singly linked list
  • The relative order of nodes in each output list must be preserved
  • Use O(1) auxiliary space

Function Signature

def split_odd_even(head):
Interviewer

Your question is Split Linked List by Position. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.