Advertisement

Swap Nodes in Pairs - LeetCode 24 Solution

Swap Nodes in Pairs - Complete Solution Guide

Swap Nodes in Pairs is LeetCode problem 24, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) Example 1: Input: head = [1,2,3,4] Output: [2,1,4,3] Explanation: Example 2: Input: head = [] Output: [] Example 3: Input: head = [1] Output: [1] Example 4: Input: head = [1,2,3] Output: [2,1,3] Constraints: The number of nodes in the list is in the range [0, 100] . 0 <= Node.val <= 100

Detailed Explanation

The problem asks us to swap every two adjacent nodes in a linked list without modifying the values stored in the nodes. We are only allowed to rearrange the nodes themselves. The input is the head of the linked list, and the output is the head of the modified linked list with nodes swapped in pairs.

Solution Approach

The solution uses an iterative approach to traverse the linked list, swapping pairs of nodes as it goes. A dummy node is introduced to simplify the handling of the list's head. The algorithm maintains a 'prev' pointer that always points to the node before the current pair being swapped. The 'next' pointers of 'prev', 'first_node', and 'second_node' are then re-assigned to effect the swap. 'prev' is then moved forward to the 'first_node' to process the next pair.

Step-by-Step Algorithm

  1. Step 1: Create a dummy node and set its 'next' pointer to the original head of the linked list. This simplifies handling the case where the head of the list needs to be changed.
  2. Step 2: Initialize a 'prev' pointer to the dummy node. This pointer is used to keep track of the node before the pair being swapped.
  3. Step 3: Iterate through the linked list while 'prev.next' and 'prev.next.next' are not null. This ensures that there are at least two nodes to swap.
  4. Step 4: Inside the loop, identify the first node ('first_node') as 'prev.next' and the second node ('second_node') as 'first_node.next'.
  5. Step 5: Perform the swap by reassigning the 'next' pointers: 'prev.next' points to 'second_node', 'first_node.next' points to 'second_node.next', and 'second_node.next' points to 'first_node'.
  6. Step 6: Move the 'prev' pointer to 'first_node' to prepare for the next swap.
  7. Step 7: After the loop finishes, return 'dummy.next', which is the head of the modified linked list.

Key Insights

  • Insight 1: The core of the solution lies in manipulating the 'next' pointers of the nodes to achieve the swapping without actually changing their values.
  • Insight 2: Using a dummy node simplifies the handling of the head of the list, especially when the first pair needs to be swapped, as it provides a stable starting point.
  • Insight 3: The solution needs to carefully handle edge cases where the list is empty or contains only one node, and when the list has an odd number of nodes.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Linked List, Recursion.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Meta, Microsoft, Nutanix, Qualcomm, Snowflake, TikTok, Uber.