How to swap nodes between two Doubly Linked List completely

Solution for How to swap nodes between two Doubly Linked List completely
is Given Below:

Hey everyone I am trying to swap two nodes between two Doubly Linked Lists completely (value and address too). Only the Nodes that are at the same positions can be swapped between i.e a Node at position 2 can only be swapped by the Node in position 2 in the other LinkedList. Consider the following example of 2 LinkedLists:

815 102 162 524 
622 101 830 754

Let’s say we want to swap the third elements which would be 162 and 830. After swapping the LinkedLists becomes:

815 102 830 524 
622 101 162 754

I have tried the following code but it doesn’t swap for the previous elements.

void swapNodes(Node* firstListNode, Node* secondListNode)
{
    Node* FirstNodeNext = firstListNode->next;
    Node* FirstNodePrev = firstListNode->previous;
    Node* SecondNodeNext = secondListNode->next;
    Node* SecondNodePrev = secondListNode->previous;
    
    //if the nodes are heads
    if (firstListNode->previous == NULL && secondListNode->previous == NULL)
    {   
        firstListNode->next = SecondNodeNext;
        secondListNode->next = FirstNodeNext;
    }
    // if the nodes are tails
    else if(firstListNode->next == NULL && secondListNode->next == NULL)
    {
        firstListNode->previous = SecondNodePrev;
        secondListNode->previous = FirstNodePrev;
    }
    else
    {
        firstListNode->next = SecondNodeNext;
        firstListNode->previous = SecondNodePrev;

        secondListNode->next = FirstNodeNext;
        secondListNode->previous = FirstNodePrev;
     }
}

How can I accomplish this task?

The else if doesn’t swap the previous elements, for example if we pass the value 524, and 754 to the function it should be the tail and execute the else if statement as it has no next nodes.

After the swapping it should be:

815 102 162 754
622 101 830 524

The code doesn’t swap the previous nodes.

I think you are losing the references for swapping in other linkedlist, below is the working code snippet for the same in java. Please note, i’m assuming here that both of the linkedlist is of same length.

public class Application {

    static class Node{
        private int value;
        private Node prev;
        private Node next;

        public Node(int value){
            this.value = value;
        }

        public void print(){
            for(Node node=this; node!=null; node = node.next){
                System.out.print(node.value + "->");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Node head1 = new Node(815);
        addNodeAtLast(head1, 102);
        addNodeAtLast(head1, 162);
        addNodeAtLast(head1, 524);
        head1.print();

        Node head2 = new Node(622);
        addNodeAtLast(head2, 101);
        addNodeAtLast(head2, 830);
        addNodeAtLast(head2, 754);
        head2.print();

        swapAtIndex(head1, head2, 3);
        head1.print();
        head2.print();
    }

    private static void swapAtIndex(Node head1, Node head2, int index){
        System.out.println("Swaping node at index : "+index);
        if(index == 0){
            Node tmp = head1.next;
            head1.next= head2.next;
            head2.next = tmp;
            return;
        }
        Node linkedList1Ref = head1,  linkedList2Ref = head2;
        for(int i=0; i!=index; ++i, linkedList1Ref = linkedList1Ref.next, linkedList2Ref=linkedList2Ref.next);

        Node temp2Prev = linkedList2Ref.prev;
        Node temp2Nxt = linkedList2Ref.next;

        linkedList1Ref.prev.next = linkedList2Ref; // LinkedList 1 swap
        linkedList2Ref.prev = linkedList1Ref.prev; // LinkedList 1 swap
        if(linkedList2Ref.next != null && linkedList1Ref.next != null) {
            linkedList2Ref.next = linkedList1Ref.next; // LinkedList 1 swap
            linkedList1Ref.next.prev = linkedList2Ref; // LinkedList 1 swap
        }

        temp2Prev.next = linkedList1Ref;
        linkedList1Ref.prev = temp2Prev;
        if(linkedList1Ref.next != null && temp2Nxt != null) {
            linkedList1Ref.next = temp2Nxt;
            temp2Nxt.prev = linkedList1Ref;
        }
    }

    private static void addNodeAtLast(Node head, int value){
        Node temp = new Node(value);
        Node prev = null;
        for(prev=head; prev.next != null; prev=prev.next);
        temp.prev = prev;
        prev.next = temp;
    }
}