VOOZH about

URL: https://www.geeksforgeeks.org/dsa/swap-nodes-in-a-linked-list-without-swapping-data/

⇱ Swap nodes in a linked list without swapping data - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Swap nodes in a linked list without swapping data

Last Updated : 20 Sep, 2024

Given a singly linked list with two values x and y, the task is to swap two nodes having values x and y without swapping data.

Examples:

Input: 1->2->3->4->5, x = 2, y = 4
Output: 1->4->3->2->5

πŸ‘ Swap-nodes-in-a-linked-list-without-swapping-data


Input: 10->15->12->13->20->14,  x = 10, y = 20
Output: 20->15->12->13->10->14

Possible cases to handle:

When swapping 2 nodes, and y, in a linked list, there are a few cases that can arise:

  • x and y are adjacent to each other, and
    • One of x or y is the head node of the list.
    • None of x or y is either a head node or a tail node.
  • x and y are not adjacent to each other, and:
    • One of x or y is the head node of the list.
    • None of x or y is either a head node or a tail node.
  • x or y don’t even exist in the linked list.

Therefore, our solution must handle all these cases.

[Naive Approach] By Keeping Track of Previous Node - O(n) Time and O(1) Space:

The idea is to first search x and y nodes in the given linked list. If any of them is not present, then return. While searching for x and y, keep track of current and previous pointers. First change next of previous pointers, then change next of current pointers.

Below is the diagram of the case where either Node X or Node Y is the head node of the list.

πŸ‘ Swap-nodes-in-a-linked-list-without-swapping-data_1
Swapping when Node X is the head of the List.


Below is the diagram of the case where Nodes X and Node Y are both not the head node of the list.

πŸ‘ Swap-nodes-in-a-linked-list-without-swapping-data-2
Swapping when neither Node X or Node Y is the head of the list.


Below is the implementation of the above approach:


Output
1 2 4 3 5 

Time Complexity: O(n), where n is the number of nodes in linked list.
Auxiliary Space: O(1)

[Efficient Approach] Using Single traversal - O(n) Time and O(1) Space:

The above code can be optimized to searchxandyin a single traversal. Two loops are used to keep the program simple.

Follow the steps below to solve the problem:

  • If x and y are the same, return as no need to swap.
  • Start traversing the List to search for x and y nodes by keeping track of the previous nodes prevX and prevY.
  • If either x or y is not found in the list, return without making any changes.
  • else, update connections:
    • If x is not the head, set prevX->next to currY. Otherwise, set currY as the new head.
    • If y is not the head, set prevY->next to currX. Otherwise, set currX as the new head.
  • Swap the next pointers of currX and currY to complete the swap.

Below is the implementation of the above approach:


Output
1 2 4 3 5 

Time Complexity: O(n), where n is the number of nodes in linked list.
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: