Practice/Meta/Leetcode 24. Swap Nodes in Pairs
CodingOptional
Given the head of a singly linked list, swap every two adjacent nodes and return the modified list's head. You must solve the problem by modifying the node links directly—do not alter the node values themselves.
For example, if the list is 1 → 2 → 3 → 4, you should return 2 → 1 → 4 → 3.
Example 1:
Input: head = [1,2,3,4] Output: [2,1,4,3] Explanation: We swap (1,2) to get 2→1, then swap (3,4) to get 4→3
Example 2:
Input: head = [1,2,3] Output: [2,1,3] Explanation: Swap the first pair (1,2), but node 3 has no pair so it stays at the end
Example 3:
Input: head = [1] Output: [1] Explanation: A single node cannot be swapped
Example 4:
Input: head = [] Output: [] Explanation: An empty list remains empty