Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Odd-Even Linked List Reordering

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

Your question is Odd-Even Linked List Reordering. 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

Loan records processed by Freddie Mac's Loan Product Advisor are represented as a singly linked list in arrival order. Reorder the list so that all nodes currently at odd 1-based positions appear first, followed by all nodes currently at even 1-based positions.

The relative order within the odd-position group and within the even-position group must remain unchanged. Return the head of the reordered list. You must rearrange existing next pointers, not create replacement nodes for the records.

Formal Specification

Implement odd_even_list(head), where head is either None or the first ListNode in a singly linked list. Each node has an integer val field and a next field containing another ListNode or None. Return the head of the same nodes after reordering. Test cases represent linked lists as arrays of node values, and expected outputs use the same representation.

Positions are 1-based. Therefore, the first node belongs to the odd group, the second belongs to the even group, and so on.

Constraints

  • 0 <= n <= 10^5
  • -10^9 <= node.val <= 10^9
  • Node values may repeat
  • The input is a singly linked list
  • Rearrange existing nodes only
  • Use O(1) auxiliary space

Function Signature

def odd_even_list(head):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output