Javatpoint Logo
Javatpoint Logo

Program to swap nodes in a singly linked list without swapping data

Explanation

In this program, we need to swap given two nodes in the singly linked list without swapping data.

Program to swap nodes in a singly linked list without swapping data

One of the approaches to accomplish this task is to swap the previous nodes of the given two nodes and then, swap the next nodes of two nodes.

Algorithm

  1. Create a class Node which has two attributes: data and next. Next is a pointer to the next node in the list.
  2. Create another class SwapNodes which has two attributes: head and tail.
  3. addNode() will add a new node to the list:
    1. Create a new node.
    2. It first checks, whether the head is equal to null which means the list is empty.
    3. If the list is empty, both head and tail will point to a newly added node.
    4. If the list is not empty, the new node will be added to end of the list such that tail's next will point to a newly added node. This new node will become the new tail of the list.
  4. swap() will swap the given two nodes present in the list:
    1. Let n1 and n2 be the values need to be swapped.
    2. If the list is empty then, return from the function.
    3. If n1 and n2 are equal then, there will be no change in the list.
    4. If the list is not empty then, search for n1 by traversing through the list such that prevNode1 which will point to node previous to node1 and node 1 will point to the node having value n1.
    5. Similarly, search for n2 by traversing through the list such that prevNode2 which will point to node previous to node2 and node 2 will point to the node having value n2.
    6. If prevNode1 points to head then, node2 will become head of the list. Similarly, if prevNode2 points to head then, node1 will become head of the list.
    7. If prevNode1 or prevNode2 is not pointing to head then, swap the nodes such that prevNode1 will become the previous node of node2 and prevNode2 will become the previous node of node1.
    8. Now, swap next nodes of node1 and node2.
  5. display() will display the nodes present in the list:
    1. Define a node current which will initially point to the head of the list.
    2. Traverse through the list till current points to null.
    3. Display each node by making current to point to node next to it in each iteration.

Solution

Python

Output:

Original list: 
1 2 3 4 5 
List after swapping nodes: 
1 5 3 4 2 

C

Output:

Original list: 
1 2 3 4 5 
List after swapping nodes: 
1 5 3 4 2 

JAVA

Output:

Original list: 
1 2 3 4 5 
List after swapping nodes: 
1 5 3 4 2 

C#

Output:

Original list: 
1 2 3 4 5 
List after swapping nodes: 
1 5 3 4 2 

PHP

Output:

Original list: 
1 2 3 4 5 
List after swapping nodes: 
1 5 3 4 2 

Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA