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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Linked List Pair Reversal
00:00
5 left

Linked List Pair Reversal

EasyPython

Problem

Jio service components may process ordered event nodes represented as singly linked lists. Given the head of a linked list, reverse its nodes two at a time and return the new head.

For each adjacent pair, swap the nodes themselves by changing pointers, not their stored values. If the list contains an odd number of nodes, leave the final node in its original position.

Formal Specification

The input is head, either None or a reference to a singly linked ListNode with fields val and next. Return the head reference of the modified list. The input list must be rearranged in place, and no new data nodes may be created.

In the test cases, linked lists are represented as arrays for readability. The evaluation harness converts each array into a ListNode chain and converts the returned chain back into an array.

Constraints

  • 0 <= n <= 100
  • -100 <= node.val <= 100
  • The input is a singly linked list.
  • Reverse nodes by changing links, not by swapping values.
  • Use O(1) auxiliary space in the iterative solution.

Function Signature

def reverse_pairs(head):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.