← Accolite questions
CodingReported in a candidate post, Sep 2021

Pairwise swap elements of a linked list

Filed under Accolite · Also reported at Moonfrog Labs

Free read 3 of 3

Confirm if the requirement is to swap node values or pointers. Pointers are preferred for interview settings.

The question

Given a singly linked list, swap every two adjacent nodes. You must swap the nodes themselves by changing their pointers, not just their values.

Constraints

  • Time complexity: O(n)
  • Space complexity: O(1)
  • Do not modify node values.

Examples

Input
1 -> 2 -> 3 -> 4
Output
2 -> 1 -> 4 -> 3
Why
The first pair (1,2) is swapped, then the second pair (3,4) is swapped.

Approach

  1. Confirm if the requirement is to swap node values or pointers. Pointers are preferred for interview settings.

Tips

  • What they are testing: Focus on lists with an odd number of nodes and empty/single-node lists.
  • Common mistake: Failing to update the 'next' pointer of the node preceding the pair being swapped.

Related Accolite questions

Collected from public candidate posts and coding forums. Updated Nov 2026.