Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 Python 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

Your question is Linked List Pair Reversal. 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.