FastPrepSwap Adjacent Linked List Nodes
Problems/Swap Adjacent Linked List Nodes

Swap Adjacent Linked List Nodes

Nutanix logoNutanix● MediumINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report is a short account of a Nutanix software engineering intern interview process in San Jose. It lists this as one of two coding questions, without saying which round asked it. It names the task only: swap the nodes of a linked list two at a time.

The report gives no examples, limits or rule for a node left over at the end. Here a list of odd length keeps its last node in place. The list has 0 to 1000 nodes with values from -10^5 to 10^5. You relink the nodes instead of swapping their values (the tests can only see the values). You return the new head, or null for an empty list. The examples are ours.

Our version is about 90% the same as the reported question.

You are given head, the first node of a singly linked list. Swap the nodes in adjacent pairs: the 1st node with the 2nd, the 3rd with the 4th, and so on. If the list has an odd number of nodes, the last node has no partner and stays at the end.

Do the swaps by changing the next pointers, not by changing the values stored in the nodes. Return the head of the new list, or null if the list is empty.

The returned list is shown as an array of its values, with [] for an empty list. The judge can only check this order of values, so relinking the nodes is a rule for you to follow rather than something the tests can see.

Function

swapInPairs(head: ListNode) → ListNode

Examples

Example 1

head = [10,20,30,40,50,60]return = [20,10,40,30,60,50]
head
102030405060
→
return
201040306050
Moved or new

The pairs are (10, 20), (30, 40) and (50, 60). The two nodes of each pair trade places, giving 20 -> 10 -> 40 -> 30 -> 60 -> 50. The new head is the node that holds 20.

Example 2

head = [7,3,9,1,5]return = [3,7,1,9,5]
head
73915
→
return
37195
Moved or new

The pairs are (7, 3) and (9, 1), which become 3 -> 7 and 1 -> 9. The fifth node, 5, has no partner, so it stays last: 3 -> 7 -> 1 -> 9 -> 5.

Example 3

head = [-4]return = [-4]
head
-4
→
return
-4

There is only one node, so there is no pair to swap. The list is returned as it is.

Constraints

  • The number of nodes in the list is between 0 and 1000.
  • -10^5 <= Node.val <= 10^5

More Nutanix Phone Screen questions

  • Top K Most Frequent ValuesPHONE SCREEN · Seen Mar 2025
  • Unique Triplets Summing to ZeroPHONE SCREEN · Seen Sep 2024
  • Find the Duplicate in a Read-Only ArrayPHONE SCREEN · Seen Apr 2021
  • Kth Largest Element in an ArrayPHONE SCREEN · Seen Feb 2020
  • Kth Smallest Element in a BSTPHONE SCREEN · Seen Feb 2020
  • Outline of a Binary TreePHONE SCREEN · Seen Jan 2020
  • Print All File and Folder PathsPHONE SCREEN · Seen Dec 2019
  • Inorder Predecessor in a BSTPHONE SCREEN · Seen Jan 2019
  • K Largest and K Smallest ValuesPHONE SCREEN · Seen Jan 2019
  • Odd Numbers Up and BackPHONE SCREEN · Seen Jan 2019

Similar questions at other companies

  • Flatten a Multilevel Doubly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Sort a Linked ListAdobe · PHONE SCREENAdobe interview questions
  • Reverse a Linked ListCisco · PHONE SCREENCisco interview questions
  • Reverse a Linked ListApple · PHONE SCREENApple interview questions
  • Reverse a Singly Linked ListChubb · PHONE SCREENChubb interview questions
  • Merge K Sorted Linked ListsBharatPe · PHONE SCREEN
  • Reverse Nodes in K-GroupByteDance · PHONE SCREENByteDance interview questions
  • Reverse a Singly Linked ListExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
  • Reverse a Singly Linked ListSuperhuman · PHONE SCREENSuperhuman interview questions
  • Sorted Doubly Linked List to Balanced BST In PlaceSalesforce · ONSITE INTERVIEWSalesforce interview questions

Interview reports that mention this question

  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)

Nutanix interview experiences

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)
  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
  • Nutanix Intern Debugging Round and Interviews: 25 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapInPairs(ListNode head) {
        // Write your code here.
    }
}
Checking account…