Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Odd-Even Linked List Reordering
00:00
5 left

Odd-Even Linked List Reordering

EasyPython

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):
Interviewer

Your question is Odd-Even Linked List Reordering. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.