Swap Kth node from beginning with Kth node from end in a Doubly Linked List

Prerequisites: Doubly Linked ListĀ
Given a doubly-linked list, the task is to swap Kth node from the beginning with Kth node from the ending.
Note: Please note here the nodes are swapped and not the data in the nodes.
Examples:Ā
Input: DLL = 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6, K = 3Ā
Output: 1 2 4 3 5 6Ā
Explanation:Ā
Third node from the beginning(3) is swapped with third node from the ending(4).Input: DLL = 1 <-> 2 <-> 3 <-> 4 <-> 5, K = 1Ā
Output: 5 2 3 4 1Ā
Approach: The idea is to traverse to the Kth element from the beginning and Kth node from the ending and change the previous and next pointers. Let K1 be the Kth node from beginning and K2 be Kth node from ending. Then:Ā
- The previous node to K2 has to be changed to the previous node of K1.
- The next node to K2 has to be changed to the next node of K1.
- The previous node to K1 has to be changed to the previous node of K2.
- The next node to K1 has to be changed to the next node of K2.
Below is the implementation of the above approach:Ā
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Ā
// Node structure of the doubly linked liststruct Node {Ā Ā Ā Ā int data;Ā Ā Ā Ā Node* next;Ā Ā Ā Ā Node* previous;};Ā
// Class of the doubly linked listclass GFG {Ā Ā Ā Ā // head of the doubly linked listĀ Ā Ā Ā Node* head;Ā
Ā Ā Ā Ā // tail of the doubly linked listĀ Ā Ā Ā Node* tail;Ā
Ā Ā Ā Ā public:Ā
Ā Ā Ā Ā // Default constructorĀ Ā Ā Ā GFG()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā head = tail = NULL;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to return the head of theĀ Ā Ā Ā // doubly linked listĀ Ā Ā Ā Node* getHead()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return head;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to set the head of theĀ Ā Ā Ā // doubly linked listĀ Ā Ā Ā void setHead(Node* head)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this->head = head;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to return the tail of theĀ Ā Ā Ā // doubly linked listĀ Ā Ā Ā Node* getTail()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return tail;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to set the tail of theĀ Ā Ā Ā // doubly linked listĀ Ā Ā Ā void setTail(Node* tail)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this->tail = tail;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to replace Kth node fromĀ Ā Ā Ā // beginning with Kth node from endĀ Ā Ā Ā void swapNode(Node* headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Node* tailReference, int k)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If K is 1, then the first nodeĀ Ā Ā Ā Ā Ā Ā Ā // has to be swapped with theĀ Ā Ā Ā Ā Ā Ā Ā // last node in the doubly linked listĀ Ā Ā Ā Ā Ā Ā Ā if (k == 1) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā swapFirstAndLast(headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tailReference);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If k is N, then the last nodeĀ Ā Ā Ā Ā Ā Ā Ā // has to be swapped with theĀ Ā Ā Ā Ā Ā Ā Ā // first node in the doubly linked listĀ Ā Ā Ā Ā Ā Ā Ā int nodeCount = getCount(headReference);Ā Ā Ā Ā Ā Ā Ā Ā if (k == nodeCount) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā swapFirstAndLast(headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tailReference);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the K<sup>th</sup> node fromĀ Ā Ā Ā Ā Ā Ā Ā // the beginning and K<sup>th</sup> nodeĀ Ā Ā Ā Ā Ā Ā Ā // from the ending are sameĀ Ā Ā Ā Ā Ā Ā Ā if (2 * k - 1 == nodeCount) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // fNode represents K<sup>th</sup> nodeĀ Ā Ā Ā Ā Ā Ā Ā // from the beginningĀ Ā Ā Ā Ā Ā Ā Ā Node* fNode = headReference;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i < k; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode = fNode->next;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Node* fNodePrevious = fNode->previous;Ā Ā Ā Ā Ā Ā Ā Ā Node* fNodeNext = fNode->next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // sNode represents K<sup>th</sup> nodeĀ Ā Ā Ā Ā Ā Ā Ā // from the endingĀ Ā Ā Ā Ā Ā Ā Ā Node* sNode = tailReference;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i < k; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode = sNode->previous;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Node* sNodePrevious = sNode->previous;Ā Ā Ā Ā Ā Ā Ā Ā Node* sNodeNext = sNode->next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Checking if any of the pointers is nullĀ Ā Ā Ā Ā Ā Ā Ā // and interchanging the pointersĀ Ā Ā Ā Ā Ā Ā Ā if (fNodePrevious != NULL && sNode != NULL) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNodePrevious->next = sNode;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode->previousĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = fNodePrevious;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode->next = fNodeNext;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNodeNext->previousĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = sNode;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā if (sNodePrevious != NULLĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && sNodeNext != NULL) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNodeNext->previousĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = fNode;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode->nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = sNodeNext;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNodePreviousĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ->next = fNode;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode->previousĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = sNodePrevious;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to swap the first andĀ Ā Ā Ā // last node in the doubly linked listĀ Ā Ā Ā void swapFirstAndLast(Node* headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Node* tailReference)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Node* headRef = headReference;Ā Ā Ā Ā Ā Ā Ā Ā Node* tailRef = tailReference;Ā
Ā Ā Ā Ā Ā Ā Ā Ā headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = headReference->next;Ā Ā Ā Ā Ā Ā Ā Ā tailReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = tailReference->previous;Ā
Ā Ā Ā Ā Ā Ā Ā Ā tailReference->nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = headRef;Ā Ā Ā Ā Ā Ā Ā Ā headRef->previousĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = tailReference;Ā Ā Ā Ā Ā Ā Ā Ā headRef->next = NULL;Ā Ā Ā Ā Ā Ā Ā Ā this->setTail(tailReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ->next);Ā
Ā Ā Ā Ā Ā Ā Ā Ā headReference->previousĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = tailRef;Ā Ā Ā Ā Ā Ā Ā Ā tailRef->nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = headReference;Ā Ā Ā Ā Ā Ā Ā Ā tailRef->previousĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = NULL;Ā Ā Ā Ā Ā Ā Ā Ā this->setHead(headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ->previous);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to return the number of nodesĀ Ā Ā Ā // in the linked listĀ Ā Ā Ā int getCount(Node* headReference)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int nodeCount = 0;Ā Ā Ā Ā Ā Ā Ā Ā while (headReference != NULL) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā nodeCount++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = headReference->next;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return nodeCount;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to print the Linked ListĀ Ā Ā Ā void printList(Node* headReference)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā if (headReference == NULL) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "Doubly linked list is empty";Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā != NULL) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << headReference->dataĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā << " ";Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = headReference->next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to insert a node atĀ Ā Ā Ā // the end of the doubly linked listĀ Ā Ā Ā void push(int data)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Node* newNode = new Node;Ā Ā Ā Ā Ā Ā Ā Ā newNode->data = data;Ā Ā Ā Ā Ā Ā Ā Ā newNode->next = NULL;Ā Ā Ā Ā Ā Ā Ā Ā newNode->previous = NULL;Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (head == NULL) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā head = tail = newNode;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tail->next = newNode;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā newNode->previous = tail;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tail = newNode;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }};Ā
// Driver codeint main(){Ā
Ā Ā Ā Ā // Creating an object for the classĀ Ā Ā Ā GFG list;Ā
Ā Ā Ā Ā // Adding data to the linked listĀ Ā Ā Ā list.push(1);Ā Ā Ā Ā list.push(2);Ā Ā Ā Ā list.push(3);Ā Ā Ā Ā list.push(4);Ā Ā Ā Ā list.push(5);Ā
Ā Ā Ā Ā // Calling the functionĀ Ā Ā Ā int K = 2;Ā Ā Ā Ā list.swapNode(list.getHead(),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā list.getTail(), K);Ā Ā Ā Ā list.printList(list.getHead());Ā
Ā Ā Ā Ā return 0;} |
Java
// Java implementation of the approachĀ
public class GFG {Ā
Ā Ā Ā Ā // Doubly Linked List implementationĀ Ā Ā Ā private class Node {Ā Ā Ā Ā Ā Ā Ā Ā private int data;Ā Ā Ā Ā Ā Ā Ā Ā private Node next;Ā Ā Ā Ā Ā Ā Ā Ā private Node previous;Ā
Ā Ā Ā Ā Ā Ā Ā Ā public Node(int data, Node next,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Node previous)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.data = data;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.next = next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.previous = previous;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā public int getData()Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return data;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā public void setData(int data)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.data = data;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā public Node getNext()Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return next;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā public void setNext(Node next)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.next = next;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā public Node getPrevious()Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return previous;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā public void setPrevious(Node previous)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.previous = previous;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā private Node head;Ā Ā Ā Ā private Node tail;Ā
Ā Ā Ā Ā public GFG()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this.head = null;Ā Ā Ā Ā Ā Ā Ā Ā this.tail = null;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā public Node getHead()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return head;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā public void setHead(Node head)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this.head = head;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā public Node getTail()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return tail;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā public void setTail(Node tail)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this.tail = tail;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to replace Kth node fromĀ Ā Ā Ā // beginning with Kth node from endĀ Ā Ā Ā public void swapNode(Node headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Node tailReference, int k)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If K is 1, then the first nodeĀ Ā Ā Ā Ā Ā Ā Ā // has to be swapped with theĀ Ā Ā Ā Ā Ā Ā Ā // last node in the doubly linked listĀ Ā Ā Ā Ā Ā Ā Ā if (k == 1) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā swapFirstAndLast(headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tailReference);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If k is N, then the last nodeĀ Ā Ā Ā Ā Ā Ā Ā // has to be swapped with theĀ Ā Ā Ā Ā Ā Ā Ā // first node in the doubly linked listĀ Ā Ā Ā Ā Ā Ā Ā int nodeCount = getCount(headReference);Ā Ā Ā Ā Ā Ā Ā Ā if (k == nodeCount) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā swapFirstAndLast(headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tailReference);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the K<sup>th</sup> node fromĀ Ā Ā Ā Ā Ā Ā Ā // the beginning and K<sup>th</sup> nodeĀ Ā Ā Ā Ā Ā Ā Ā // from the ending are sameĀ Ā Ā Ā Ā Ā Ā Ā if (2 * k - 1 == nodeCount) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // fNode represents K<sup>th</sup> nodeĀ Ā Ā Ā Ā Ā Ā Ā // from the beginningĀ Ā Ā Ā Ā Ā Ā Ā Node fNode = headReference;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i < k; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode = fNode.getNext();Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Node fNodePrevious = fNode.getPrevious();Ā Ā Ā Ā Ā Ā Ā Ā Node fNodeNext = fNode.getNext();Ā
Ā Ā Ā Ā Ā Ā Ā Ā // sNode represents K<sup>th</sup> nodeĀ Ā Ā Ā Ā Ā Ā Ā // from the endingĀ Ā Ā Ā Ā Ā Ā Ā Node sNode = tailReference;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i < k; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode = sNode.getPrevious();Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Node sNodePrevious = sNode.getPrevious();Ā Ā Ā Ā Ā Ā Ā Ā Node sNodeNext = sNode.getNext();Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Checking if any of the pointers is nullĀ Ā Ā Ā Ā Ā Ā Ā // and interchanging the pointersĀ Ā Ā Ā Ā Ā Ā Ā if (fNodePrevious != null && sNode != null) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNodePrevious.setNext(sNode);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode.setPrevious(fNodePrevious);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode.setNext(fNodeNext);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNodeNext.setPrevious(sNode);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā if (sNodePrevious != null && sNodeNext != null) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNodeNext.setPrevious(fNode);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode.setNext(sNodeNext);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNodePrevious.setNext(fNode);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode.setPrevious(sNodePrevious);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to swap the first andĀ Ā Ā Ā // last node in the doubly linked listĀ Ā Ā Ā private void swapFirstAndLast(Ā Ā Ā Ā Ā Ā Ā Ā Node headReference,Ā Ā Ā Ā Ā Ā Ā Ā Node tailReference)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Node headRef = headReference;Ā Ā Ā Ā Ā Ā Ā Ā Node tailRef = tailReference;Ā
Ā Ā Ā Ā Ā Ā Ā Ā headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = headReference.getNext();Ā Ā Ā Ā Ā Ā Ā Ā tailReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = tailReference.getPrevious();Ā
Ā Ā Ā Ā Ā Ā Ā Ā tailReference.setNext(headRef);Ā Ā Ā Ā Ā Ā Ā Ā headRef.setPrevious(tailReference);Ā Ā Ā Ā Ā Ā Ā Ā headRef.setNext(null);Ā Ā Ā Ā Ā Ā Ā Ā this.setTail(tailReference.getNext());Ā
Ā Ā Ā Ā Ā Ā Ā Ā headReference.setPrevious(tailRef);Ā Ā Ā Ā Ā Ā Ā Ā tailRef.setNext(headReference);Ā Ā Ā Ā Ā Ā Ā Ā tailRef.setPrevious(null);Ā Ā Ā Ā Ā Ā Ā Ā this.setHead(headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .getPrevious());Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to return the number of nodesĀ Ā Ā Ā // in the linked listĀ Ā Ā Ā private int getCount(Node headReference)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int nodeCount = 0;Ā Ā Ā Ā Ā Ā Ā Ā while (headReference != null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā nodeCount++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReference = headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .getNext();Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return nodeCount;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to print the Linked ListĀ Ā Ā Ā public void printList(Node headReference)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā if (headReference == null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Doubly linked list is empty");Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (headReference != null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.print(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReference.getData()Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + " ");Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = headReference.getNext();Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to insert a node atĀ Ā Ā Ā // the end of the doubly linked listĀ Ā Ā Ā public void push(int data)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Node newNodeĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new Node(data, null, null);Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (head == null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā head = tail = newNode;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tail.setNext(newNode);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā newNode.setPrevious(tail);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tail = newNode;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Creating an object for the classĀ Ā Ā Ā Ā Ā Ā Ā GFG list = new GFG();Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Adding data to the linked listĀ Ā Ā Ā Ā Ā Ā Ā list.push(1);Ā Ā Ā Ā Ā Ā Ā Ā list.push(2);Ā Ā Ā Ā Ā Ā Ā Ā list.push(3);Ā Ā Ā Ā Ā Ā Ā Ā list.push(4);Ā Ā Ā Ā Ā Ā Ā Ā list.push(5);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calling the functionĀ Ā Ā Ā Ā Ā Ā Ā int K = 2;Ā Ā Ā Ā Ā Ā Ā Ā list.swapNode(list.getHead(),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā list.getTail(), K);Ā Ā Ā Ā Ā Ā Ā Ā list.printList(list.getHead());Ā Ā Ā Ā }} |
Python3
# Python implementation of the approachĀ
# Node structure of the doubly linked listclass Node:Ā Ā Ā Ā def __init__(self, data):Ā Ā Ā Ā Ā Ā Ā Ā self.data = dataĀ Ā Ā Ā Ā Ā Ā Ā self.next = NoneĀ Ā Ā Ā Ā Ā Ā Ā self.previous = NoneĀ
Ā Ā Ā Ā def getData(self):Ā Ā Ā Ā Ā Ā Ā Ā return self.dataĀ
Ā Ā Ā Ā def setData(self, data):Ā Ā Ā Ā Ā Ā Ā Ā self.data = dataĀ
Ā Ā Ā Ā def getNext(self, ):Ā Ā Ā Ā Ā Ā Ā Ā return nextĀ
Ā Ā Ā Ā def setNext(self, next):Ā Ā Ā Ā Ā Ā Ā Ā self.next = nextĀ
Ā Ā Ā Ā def getPrevious(self):Ā Ā Ā Ā Ā Ā Ā Ā return previousĀ
Ā Ā Ā Ā def setPrevious(self, previous):Ā Ā Ā Ā Ā Ā Ā Ā self.previous = previousĀ
# Class of the doubly linked listclass GFG:Ā Ā Ā Ā # head of the doubly linked listĀ Ā Ā Ā def __init__(self):Ā Ā Ā Ā Ā Ā Ā Ā self.head = NoneĀ Ā Ā Ā Ā Ā Ā Ā # tail of the doubly linked listĀ Ā Ā Ā Ā Ā Ā Ā self.tail = NoneĀ
Ā Ā Ā Ā # Function to return the head of the doubly linked listĀ Ā Ā Ā def getHead(self):Ā Ā Ā Ā Ā Ā Ā Ā return self.headĀ
Ā Ā Ā Ā # Function to set the head of the doubly linked listĀ Ā Ā Ā def setHead(self, head):Ā Ā Ā Ā Ā Ā Ā Ā self.head = headĀ
Ā Ā Ā Ā # Function to return the tail of the doubly linked listĀ Ā Ā Ā def getTail(self):Ā Ā Ā Ā Ā Ā Ā Ā return self.tailĀ
Ā Ā Ā Ā # Function to set the tail of the doubly linked listĀ Ā Ā Ā def setTail(self, tail):Ā Ā Ā Ā Ā Ā Ā Ā self.tail = tailĀ
Ā Ā Ā Ā # Function to replace Kth node from beginning with Kth node from endĀ Ā Ā Ā def swapNode(self, headReference, tailReference, k):Ā
Ā Ā Ā Ā Ā Ā Ā Ā # If K is 1, then the first node has to be swapped with the last node in the doubly linked listĀ Ā Ā Ā Ā Ā Ā Ā if k == 1:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.swapFirstAndLast(headReference, tailReference)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā returnĀ
Ā Ā Ā Ā Ā Ā Ā Ā # If k is N, then the last node has to be swapped with the first node in the doubly linked listĀ Ā Ā Ā Ā Ā Ā Ā nodeCount = self.getCount(headReference)Ā Ā Ā Ā Ā Ā Ā Ā if k == nodeCount:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.swapFirstAndLast(headReference, tailReference)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā returnĀ
Ā Ā Ā Ā Ā Ā Ā Ā # If the Kth node from the beginning and Kth node from the ending are sameĀ Ā Ā Ā Ā Ā Ā Ā if 2 * k - 1 == nodeCount:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā returnĀ
Ā Ā Ā Ā Ā Ā Ā Ā # fNode represents Kth node from the beginningĀ Ā Ā Ā Ā Ā Ā Ā fNode = headReferenceĀ Ā Ā Ā Ā Ā Ā Ā for i in range(1, k):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode = fNode.nextĀ Ā Ā Ā Ā Ā Ā Ā fNodePrevious = fNode.previousĀ Ā Ā Ā Ā Ā Ā Ā fNodeNext = fNode.nextĀ
Ā Ā Ā Ā Ā Ā Ā Ā # sNode represents Kth node from the endingĀ Ā Ā Ā Ā Ā Ā Ā sNode = tailReferenceĀ Ā Ā Ā Ā Ā Ā Ā for i in range(1, k):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode = sNode.previousĀ
Ā Ā Ā Ā Ā Ā Ā Ā sNodePrevious = sNode.previousĀ Ā Ā Ā Ā Ā Ā Ā sNodeNext = sNode.nextĀ
Ā Ā Ā Ā Ā Ā Ā Ā # Checking if any of the pointers is null and interchanging the pointersĀ Ā Ā Ā Ā Ā Ā Ā if fNodePrevious != None and sNode != None:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNodePrevious.next = sNodeĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode.previous = fNodePreviousĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode.next = fNodeNextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNodeNext.previous = sNodeĀ Ā Ā Ā Ā Ā Ā Ā if sNodePrevious != None and sNodeNext != None:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNodeNext.previous = fNodeĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode.next = sNodeNextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNodePrevious.next = fNodeĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode.previous = sNodePreviousĀ
Ā Ā Ā Ā # Function to swap the first and last node in the doubly linked listĀ Ā Ā Ā def swapFirstAndLast(self, headReference, tailReference):Ā Ā Ā Ā Ā Ā Ā Ā headRef = headReferenceĀ Ā Ā Ā Ā Ā Ā Ā tailRef = tailReferenceĀ
Ā Ā Ā Ā Ā Ā Ā Ā headReference = headReference.nextĀ Ā Ā Ā Ā Ā Ā Ā tailReference = tailReference.previousĀ
Ā Ā Ā Ā Ā Ā Ā Ā tailReference.next = headRefĀ Ā Ā Ā Ā Ā Ā Ā headRef.previous = tailReferenceĀ Ā Ā Ā Ā Ā Ā Ā headRef.next = NoneĀ Ā Ā Ā Ā Ā Ā Ā self.setTail(tailReference.next)Ā
Ā Ā Ā Ā Ā Ā Ā Ā headReference.previous = tailRefĀ Ā Ā Ā Ā Ā Ā Ā tailRef.next = headReferenceĀ Ā Ā Ā Ā Ā Ā Ā tailRef.previous = NoneĀ Ā Ā Ā Ā Ā Ā Ā self.setHead(headReference.previous)Ā
Ā Ā Ā Ā # Function to return the number of nodes in the linked listĀ Ā Ā Ā def getCount(self, headReference):Ā Ā Ā Ā Ā Ā Ā Ā nodeCount = 0Ā Ā Ā Ā Ā Ā Ā Ā while headReference != None:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā nodeCount += 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReference = headReference.nextĀ Ā Ā Ā Ā Ā Ā Ā return nodeCountĀ Ā Ā Ā Ā Ā Ā Ā Ā # Function to insert a node atĀ Ā Ā Ā # the end of the doubly linked listĀ Ā Ā Ā def push(self, data):Ā Ā Ā Ā Ā Ā Ā Ā newNode = Node(data)Ā Ā Ā Ā Ā Ā Ā Ā if (self.head == None):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.head = self.tail = newNodeĀ Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.tail.setNext(newNode)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā newNode.setPrevious(self.tail)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.tail = newNodeĀ Ā Ā Ā Ā Ā Ā Ā Ā def printList(self, headReference):Ā Ā Ā Ā Ā Ā Ā Ā if headReference == None: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print("Doubly linked list is empty")Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā returnĀ Ā Ā Ā Ā Ā Ā Ā while headReference:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print(headReference.data,end = ' ')Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReference = headReference.nextĀ Ā Ā Ā Ā Ā Ā Ā print()Ā Ā Ā Ā Ā # Driver code# Creating an object for the classlist = GFG()Ā
# Adding data to the linked listlist.push(1)list.push(2)list.push(3)list.push(4)list.push(5)Ā
# Calling the functionK = 2list.swapNode(list.getHead(),list.getTail(), K)list.printList(list.getHead()) |
C#
// C# implementation of the approachusing System;public class GFG {Ā Ā Ā Ā Ā Ā // Doubly Linked List implementationĀ Ā Ā Ā private class Node {Ā Ā Ā Ā Ā Ā Ā Ā private int data;Ā Ā Ā Ā Ā Ā Ā Ā private Node next;Ā Ā Ā Ā Ā Ā Ā Ā private Node previous;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public Node(int data, Node next,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Node previous)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.data = data;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.next = next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.previous = previous;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public int getData()Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return data;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public void setData(int data)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.data = data;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public Node getNext()Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return next;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public void setNext(Node next)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.next = next;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public Node getPrevious()Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return previous;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public void setPrevious(Node previous)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.previous = previous;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā private Node head;Ā Ā Ā Ā private Node tail;Ā Ā Ā Ā Ā Ā public GFG()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this.head = null;Ā Ā Ā Ā Ā Ā Ā Ā this.tail = null;Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Node getHead()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return head;Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā void setHead(Node head)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this.head = head;Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Node getTail()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return tail;Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā void setTail(Node tail)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this.tail = tail;Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā // Function to replace Kth node fromĀ Ā Ā Ā // beginning with Kth node from endĀ Ā Ā Ā void swapNode(Node headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Node tailReference, int k)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If K is 1, then the first nodeĀ Ā Ā Ā Ā Ā Ā Ā // has to be swapped with theĀ Ā Ā Ā Ā Ā Ā Ā // last node in the doubly linked listĀ Ā Ā Ā Ā Ā Ā Ā if (k == 1) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā swapFirstAndLast(headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tailReference);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If k is N, then the last nodeĀ Ā Ā Ā Ā Ā Ā Ā // has to be swapped with theĀ Ā Ā Ā Ā Ā Ā Ā // first node in the doubly linked listĀ Ā Ā Ā Ā Ā Ā Ā int nodeCount = getCount(headReference);Ā Ā Ā Ā Ā Ā Ā Ā if (k == nodeCount) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā swapFirstAndLast(headReference,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tailReference);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the K<sup>th</sup> node fromĀ Ā Ā Ā Ā Ā Ā Ā // the beginning and K<sup>th</sup> nodeĀ Ā Ā Ā Ā Ā Ā Ā // from the ending are sameĀ Ā Ā Ā Ā Ā Ā Ā if (2 * k - 1 == nodeCount) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // fNode represents K<sup>th</sup> nodeĀ Ā Ā Ā Ā Ā Ā Ā // from the beginningĀ Ā Ā Ā Ā Ā Ā Ā Node fNode = headReference;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i < k; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode = fNode.getNext();Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Node fNodePrevious = fNode.getPrevious();Ā Ā Ā Ā Ā Ā Ā Ā Node fNodeNext = fNode.getNext();Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // sNode represents K<sup>th</sup> nodeĀ Ā Ā Ā Ā Ā Ā Ā // from the endingĀ Ā Ā Ā Ā Ā Ā Ā Node sNode = tailReference;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i < k; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode = sNode.getPrevious();Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Node sNodePrevious = sNode.getPrevious();Ā Ā Ā Ā Ā Ā Ā Ā Node sNodeNext = sNode.getNext();Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Checking if any of the pointers is nullĀ Ā Ā Ā Ā Ā Ā Ā // and interchanging the pointersĀ Ā Ā Ā Ā Ā Ā Ā if (fNodePrevious != null && sNode != null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNodePrevious.setNext(sNode);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode.setPrevious(fNodePrevious);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNode.setNext(fNodeNext);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNodeNext.setPrevious(sNode);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā if (sNodePrevious != null && sNodeNext != null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNodeNext.setPrevious(fNode);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode.setNext(sNodeNext);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sNodePrevious.setNext(fNode);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fNode.setPrevious(sNodePrevious);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā // Function to swap the first andĀ Ā Ā Ā // last node in the doubly linked listĀ Ā Ā Ā private void swapFirstAndLast(Ā Ā Ā Ā Ā Ā Ā Ā Node headReference,Ā Ā Ā Ā Ā Ā Ā Ā Node tailReference)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Node headRef = headReference;Ā Ā Ā Ā Ā Ā Ā Ā Node tailRef = tailReference;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = headReference.getNext();Ā Ā Ā Ā Ā Ā Ā Ā tailReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = tailReference.getPrevious();Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tailReference.setNext(headRef);Ā Ā Ā Ā Ā Ā Ā Ā headRef.setPrevious(tailReference);Ā Ā Ā Ā Ā Ā Ā Ā headRef.setNext(null);Ā Ā Ā Ā Ā Ā Ā Ā this.setTail(tailReference.getNext());Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReference.setPrevious(tailRef);Ā Ā Ā Ā Ā Ā Ā Ā tailRef.setNext(headReference);Ā Ā Ā Ā Ā Ā Ā Ā tailRef.setPrevious(null);Ā Ā Ā Ā Ā Ā Ā Ā this.setHead(headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .getPrevious());Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā // Function to return the number of nodesĀ Ā Ā Ā // in the linked listĀ Ā Ā Ā private int getCount(Node headReference)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int nodeCount = 0;Ā Ā Ā Ā Ā Ā Ā Ā while (headReference != null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā nodeCount++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReference = headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .getNext();Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return nodeCount;Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā // Function to print the Linked ListĀ Ā Ā Ā void printList(Node headReference)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā if (headReference == null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Doubly linked list is empty");Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (headReference != null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReference.getData()Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + " ");Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā headReferenceĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = headReference.getNext();Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā // Function to insert a node atĀ Ā Ā Ā // the end of the doubly linked listĀ Ā Ā Ā void Push(int data)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Node newNodeĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new Node(data, null, null);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (head == null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā head = tail = newNode;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tail.setNext(newNode);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā newNode.setPrevious(tail);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tail = newNode;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā public static void Main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Creating an object for the classĀ Ā Ā Ā Ā Ā Ā Ā GFG list = new GFG();Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Adding data to the linked listĀ Ā Ā Ā Ā Ā Ā Ā list.Push(1);Ā Ā Ā Ā Ā Ā Ā Ā list.Push(2);Ā Ā Ā Ā Ā Ā Ā Ā list.Push(3);Ā Ā Ā Ā Ā Ā Ā Ā list.Push(4);Ā Ā Ā Ā Ā Ā Ā Ā list.Push(5);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Calling the functionĀ Ā Ā Ā Ā Ā Ā Ā int K = 2;Ā Ā Ā Ā Ā Ā Ā Ā list.swapNode(list.getHead(),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā list.getTail(), K);Ā Ā Ā Ā Ā Ā Ā Ā list.printList(list.getHead());Ā Ā Ā Ā }}Ā
// This code is contributed by 29AjayKumar |
1 4 3 2 5
Time Complexity: O(K), As we are traversing till Kth element from tail and head of the list and then changing the links which is a O(1) operation.
Auxiliary Space: O(1), As constant extra space is used.
Method 2: Without swapping the elements and without using temporary node.
Approach: There are 3 cases in order to swap the nodes.
- Swapping the first and the last nodes (k = 1)
- Swapping the ordinary Kth node from the beginning and Kth node from the end.
- Swapping middle nodes
Case 1: Swap first and last nodes (k = 1)
Steps:
- Make the list as a circular linked list
- Change the previous pointer of the first node to the last but one node (20 in example figure)
- Change the next pointer of last but one node to the last node. In this case it will be 60.
- After swapping, make the head as the first node.
Consider p and q are the nodes which are to be swapped, head = q; //change head pointer to point to head node last = p; //change last pointer to point to last node
swapping first and last nodes
Case 2: Swapping the ordinary Kth node from the beginning and Kth node from the end.
Steps:
- Let us consider K = 2. So the nodes to be swapped or interchanged are 20 and 50 as show in the figure.
- Make both the first and next pointers of the nodes which are to be swapped to point to the previous nodes. To do this, we need to change the links of the previous nodes to point to the node which is after the node to be swapped.
Consider the nodes to be swapped are p and q:
//Change the link of the next pointer of the previous node to point to
//the next node of to be swapped node.
q.first.next = q.next;
p.first.next = p.next; // Same procedure for the other node
//Make sure to change the previous/first pointer of the next node to
//point to the previous of to be swapped node.
q.next.first = q.first;
p.next.first = p.first;
//Both the first and next pointers points to the previous node as shown in the below figure.
q.next = q.first;
p.next = p.first;
Ā Ā Ā 3. Interchange the pointers of one node to be swapped nodes with the other to be swapped node. (step 3 denotes the Ā Ā Ā Ā Ā Ā Ā Ā figure after interchanging).
Ā Ā Ā 4. Make the required changes in the links in order to make it as a complete list.
General case
Case 3: Swapping the middle nodes
Steps:
- This case is same as the case 2 and the only change is, the nodes which are to be swapped are middle nodes. So both of them are together (side-by-side).
- Consider p is the first node to be swapped and q is the second node to be swapped.
- Point the next pointer of previous node of p to the next node of q. This step is done to omit p and q nodes.
- In the same way, point the first pointer of next node of q to the previous node of q.
- Change the links of p and q so that both the nodes points to the previous node of p (step2 in the below figure).
- Make the links of p and q accordingly to make the nodes swap their positions.
To swap middle nodes
Implementation:
C++
// CPP program to swap Kth node from beginning with// the Kth node from the end without using temporary// node and without swapping the data#include <bits/stdc++.h>using namespace std;Ā
// class Nodeclass Node {public:Ā Ā Ā Ā int data;Ā Ā Ā Ā Node *first, *next;Ā Ā Ā Ā Node(int data)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this->data = data;Ā Ā Ā Ā Ā Ā Ā Ā first = NULL;Ā Ā Ā Ā Ā Ā Ā Ā next = NULL;Ā Ā Ā Ā }};Ā
// function for inserting new node at the// end of the list using last pointervoid AddLast(int data, Node*& head, Node*& last){Ā Ā Ā Ā Node* temp = new Node(data);Ā Ā Ā Ā if (!head) {Ā Ā Ā Ā Ā Ā Ā Ā head = temp;Ā Ā Ā Ā Ā Ā Ā Ā last = temp;Ā Ā Ā Ā }Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā last->next = temp;Ā Ā Ā Ā Ā Ā Ā Ā temp->first = last;Ā Ā Ā Ā Ā Ā Ā Ā last = temp;Ā Ā Ā Ā }}Ā
// function for printing the doubly linked listvoid printList(Node* head){Ā Ā Ā Ā Node* p = head;Ā Ā Ā Ā while (p) {Ā Ā Ā Ā Ā Ā Ā Ā cout << p->data << "<->";Ā Ā Ā Ā Ā Ā Ā Ā p = p->next;Ā Ā Ā Ā }Ā Ā Ā Ā cout << "NULL" << endl;}Ā
// function for swapping Kth node from// beginning with Kth node from the endvoid swapKthNodes(Node*& head, Node*& last, int k){Ā Ā Ā Ā int count = 1;Ā Ā Ā Ā Node *p = head, *q = last;Ā Ā Ā Ā // case 1: to swap the start and end nodesĀ Ā Ā Ā // case 1 figureĀ Ā Ā Ā if (k == 1) {Ā Ā Ā Ā Ā Ā Ā Ā q->first->next = p;Ā Ā Ā Ā Ā Ā Ā Ā p->first = q->first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā q->next = p->next;Ā Ā Ā Ā Ā Ā Ā Ā p->next->first = q;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // change these links to NULL to the break circularĀ Ā Ā Ā Ā Ā Ā Ā // linkĀ Ā Ā Ā Ā Ā Ā Ā p->next = NULL;Ā Ā Ā Ā Ā Ā Ā Ā q->first = NULL;Ā
Ā Ā Ā Ā Ā Ā Ā Ā head = q;Ā Ā Ā Ā Ā Ā Ā Ā last = p;Ā Ā Ā Ā }Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā while (p != NULL && q != NULL && count < k) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p = p->next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q = q->first;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // case 3: if the nodes to be swapped are the middleĀ Ā Ā Ā Ā Ā Ā Ā // nodes given in the figureĀ Ā Ā Ā Ā Ā Ā Ā if (p->next == q) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->first->next = p->next->next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->next->first = p->first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->next = p->first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->first = q->next = p->first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->next = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->next->next->first = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->next = p->first->next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->first->next = q;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->first = q;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā // case 2: other than middle nodesĀ Ā Ā Ā Ā Ā Ā Ā // given in case 2 figureĀ Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->first->next = q->next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->next->first = q->first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->next = q->first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->first->next = p->next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->next->first = p->first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->next = p->first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->first = q->first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->first = p->next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->next = p->first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->next = q->first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->next = q->next->next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->first->next = q;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q->next->first = q;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->next = p->next->next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->first->next = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p->next->first = p;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }}Ā
// Driver functionint main(){Ā Ā Ā Ā // head pointer for pointing to start of the linked listĀ Ā Ā Ā // last pointer for pointing to last node of the linkedĀ Ā Ā Ā // listĀ Ā Ā Ā Node *head = NULL, *last = NULL;Ā
Ā Ā Ā Ā // function calls for insertingĀ Ā Ā Ā // at the end of the listĀ Ā Ā Ā AddLast(10, head, last);Ā Ā Ā Ā AddLast(20, head, last);Ā Ā Ā Ā AddLast(30, head, last);Ā Ā Ā Ā AddLast(40, head, last);Ā Ā Ā Ā AddLast(50, head, last);Ā Ā Ā Ā AddLast(60, head, last);Ā
Ā Ā Ā Ā cout << "Before swapping:" << endl;Ā Ā Ā Ā // print list before swapping the nodesĀ Ā Ā Ā printList(head);Ā Ā Ā Ā cout << endl;Ā
Ā Ā Ā Ā // function call for swapping Kth nodesĀ Ā Ā Ā swapKthNodes(head, last, 1);Ā
Ā Ā Ā Ā cout << "After swapping nodes for k = 1:" << endl;Ā Ā Ā Ā // print list after swapping the nodesĀ Ā Ā Ā printList(head);Ā Ā Ā Ā cout << endl;Ā
Ā Ā Ā Ā swapKthNodes(head, last, 2);Ā Ā Ā Ā cout << "After swapping nodes for k = 2:" << endl;Ā Ā Ā Ā printList(head);Ā Ā Ā Ā cout << endl;Ā
Ā Ā Ā Ā swapKthNodes(head, last, 3);Ā Ā Ā Ā cout << "After swapping nodes for k = 3 (middle):"Ā Ā Ā Ā Ā Ā Ā Ā Ā << endl;Ā Ā Ā Ā printList(head);Ā Ā Ā Ā cout << endl;Ā Ā Ā Ā return 0;}Ā
// This code is contributed Tapesh(tapeshdua420) |
Java
// Java program to swap Kth node from beginning with// the Kth node from the end without using temporary// node and without swapping the datapublic class GFG{Ā
Ā Ā // head pointer for pointing to start of the linked listĀ Ā // last pointer for pointing to last node of the linked listĀ Ā Node head = null,last = null;Ā
Ā Ā // class NodeĀ Ā class Node{Ā Ā Ā Ā int data;Ā Ā Ā Ā Node first,next;Ā Ā Ā Ā Node(int data){Ā Ā Ā Ā Ā Ā this.data = data;Ā Ā Ā Ā Ā Ā first = null;Ā Ā Ā Ā Ā Ā next = null;Ā Ā Ā Ā }Ā Ā }Ā
Ā Ā // function for inserting new node at theĀ Ā // end of the list using last pointerĀ Ā void AddLast(int data) {Ā Ā Ā Ā Node temp = new Node(data);Ā Ā Ā Ā if(head == null) {Ā Ā Ā Ā Ā Ā head = temp;Ā Ā Ā Ā Ā Ā last = temp;Ā Ā Ā Ā }Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā last.next = temp;Ā Ā Ā Ā Ā Ā temp.first = last;Ā Ā Ā Ā Ā Ā last = temp;Ā Ā Ā Ā }Ā Ā }Ā
Ā Ā // function for printing the doubly linked listĀ Ā void printList() {Ā Ā Ā Ā Node p = head;Ā Ā Ā Ā while(p!=null) {Ā Ā Ā Ā Ā Ā System.out.print(p.data+"<->");Ā Ā Ā Ā Ā Ā p = p.next;Ā Ā Ā Ā }Ā Ā Ā Ā System.out.print("null");Ā Ā Ā Ā System.out.println();Ā Ā }Ā
Ā Ā // function for swapping Kth node from Ā Ā // beginning with Kth node from the endĀ Ā void swapKthNodes(int k) {Ā Ā Ā Ā int count = 1;Ā Ā Ā Ā Node p = head, q = last;Ā
Ā Ā Ā Ā // case 1: to swap the start and end nodesĀ Ā Ā Ā // case 1 figureĀ Ā Ā Ā if(k == 1) {Ā Ā Ā Ā Ā Ā q.first.next = p;Ā Ā Ā Ā Ā Ā p.first = q.first;Ā
Ā Ā Ā Ā Ā Ā q.next = p.next;Ā Ā Ā Ā Ā Ā p.next.first = q;Ā
Ā Ā Ā Ā Ā Ā // change these links to null to the break circular linkĀ Ā Ā Ā Ā Ā p.next = null;Ā Ā Ā Ā Ā Ā q.first = null;Ā
Ā Ā Ā Ā Ā Ā head = q;Ā Ā Ā Ā Ā Ā last = p;Ā Ā Ā Ā }Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā while(p != nullĀ &&Ā q != nullĀ &&Ā count < k) {Ā Ā Ā Ā Ā Ā Ā Ā count++;Ā Ā Ā Ā Ā Ā Ā Ā p = p.next;Ā Ā Ā Ā Ā Ā Ā Ā q = q.first;Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā // case 3: if the nodes to be swapped are the middle nodesĀ Ā Ā Ā Ā Ā // given in the figureĀ Ā Ā Ā Ā Ā if(p.next == q) {Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p.next.next;Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = p.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first;Ā Ā Ā Ā Ā Ā Ā Ā q.first = q.next = p.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā q.next = p;Ā Ā Ā Ā Ā Ā Ā Ā p.next.next.first = p;Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first.next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = q;Ā Ā Ā Ā Ā Ā Ā Ā p.first = q;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā // case 2: other than middle nodesĀ Ā Ā Ā Ā Ā // given in case 2 figureĀ Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = q.next;Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = q.first;Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p.next;Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = p.first;Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā p.first = q.first;Ā Ā Ā Ā Ā Ā Ā Ā q.first = p.next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first;Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.next.next;Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = q;Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = q;Ā
Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.next.next;Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p;Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = p;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā }Ā
Ā Ā // Driver functionĀ Ā public static void main(String args[])Ā Ā {Ā
Ā Ā Ā Ā // class objectĀ Ā Ā Ā GFG list = new GFG();Ā
Ā Ā Ā Ā // function calls for inserting Ā Ā Ā Ā // at the end of the listĀ Ā Ā Ā list.AddLast(10);Ā Ā Ā Ā list.AddLast(20);Ā Ā Ā Ā list.AddLast(30);Ā Ā Ā Ā list.AddLast(40);Ā Ā Ā Ā list.AddLast(50);Ā Ā Ā Ā list.AddLast(60);Ā
Ā Ā Ā Ā System.out.println("Before swapping:");Ā Ā Ā Ā // print list before swapping the nodesĀ Ā Ā Ā list.printList();Ā Ā Ā Ā System.out.println();Ā
Ā Ā Ā Ā // function call for swapping Kth nodesĀ Ā Ā Ā list.swapKthNodes(1);Ā
Ā Ā Ā Ā System.out.println("After swapping nodes for k = 1:");Ā
Ā Ā Ā Ā // print list after swapping the nodesĀ Ā Ā Ā list.printList();Ā Ā Ā Ā System.out.println();Ā
Ā Ā Ā Ā list.swapKthNodes(2);Ā Ā Ā Ā System.out.println("After swapping nodes for k = 2:");Ā Ā Ā Ā list.printList();Ā Ā Ā Ā System.out.println();Ā
Ā Ā Ā Ā list.swapKthNodes(3);Ā Ā Ā Ā System.out.println("After swapping nodes for k = 3 (middle):");Ā Ā Ā Ā list.printList();Ā Ā Ā Ā System.out.println();Ā Ā }}Ā
// This code is contributed by Likhita AVL |
Python3
# Python program to swap Kth node from beginning with# the Kth node from the end without using temporary# node and without swapping the dataĀ
# class Nodeclass Node:Ā Ā Ā Ā def __init__(self, data):Ā Ā Ā Ā Ā Ā Ā Ā self.data = dataĀ Ā Ā Ā Ā Ā Ā Ā self.first = NoneĀ Ā Ā Ā Ā Ā Ā Ā self.next = NoneĀ
Ā
class GFG:Ā Ā Ā Ā # head pointer for pointing to start of the linked listĀ Ā Ā Ā # last pointer for pointing to last node of the linked listĀ Ā Ā Ā def __init__(self):Ā Ā Ā Ā Ā Ā Ā Ā self.head = NoneĀ Ā Ā Ā Ā Ā Ā Ā self.last = NoneĀ
Ā Ā Ā Ā # function for inserting new node at theĀ Ā Ā Ā # end of the list using last pointerĀ Ā Ā Ā def AddLast(self, data):Ā Ā Ā Ā Ā Ā Ā Ā temp = Node(data)Ā Ā Ā Ā Ā Ā Ā Ā if self.head == None:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.head = tempĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.last = tempĀ Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.last.next = tempĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp.first = self.lastĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.last = tempĀ
Ā Ā Ā Ā # function for printing the doubly linked listĀ Ā Ā Ā def printList(self):Ā Ā Ā Ā Ā Ā Ā Ā p = self.headĀ Ā Ā Ā Ā Ā Ā Ā while p != None:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print(p.data, "<->", end="")Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p = p.nextĀ Ā Ā Ā Ā Ā Ā Ā print("null")Ā
Ā Ā Ā Ā # function for swapping Kth node fromĀ Ā Ā Ā # beginning with Kth node from the endĀ Ā Ā Ā def swapKthNodes(self, k):Ā Ā Ā Ā Ā Ā Ā Ā count = 1Ā Ā Ā Ā Ā Ā Ā Ā p = self.headĀ Ā Ā Ā Ā Ā Ā Ā q = self.lastĀ
Ā Ā Ā Ā Ā Ā Ā Ā # case 1: to swap the start and end nodesĀ Ā Ā Ā Ā Ā Ā Ā # case 1 figureĀ Ā Ā Ā Ā Ā Ā Ā if k == 1:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = pĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first = q.firstĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = p.nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = qĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # change these links to null to the break circular linkĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = NoneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first = NoneĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.head = qĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.last = pĀ
Ā Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while p != None and q != None and count < k:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count += 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p = p.nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q = q.firstĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # case 3: if the nodes to be swapped are the middle nodesĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # given in the figureĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if p.next == q:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p.next.nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = p.firstĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.firstĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first = q.next = p.firstĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = pĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.next.first = pĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first.nextĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = qĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first = qĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # case 2: other than middle nodesĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # given in case 2 figureĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = q.nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = q.firstĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.firstĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p.nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = p.firstĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.firstĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first = q.firstĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first = p.nextĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.firstĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.firstĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.next.nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = qĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = qĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.next.nextĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = pĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = pĀ
Ā
# Driver functionif __name__ == '__main__':Ā Ā Ā Ā # class objectĀ Ā Ā Ā list = GFG()Ā
Ā Ā Ā Ā # function calls for insertingĀ Ā Ā Ā # at the end of the listĀ Ā Ā Ā list.AddLast(10)Ā Ā Ā Ā list.AddLast(20)Ā Ā Ā Ā list.AddLast(30)Ā Ā Ā Ā list.AddLast(40)Ā Ā Ā Ā list.AddLast(50)Ā Ā Ā Ā list.AddLast(60)Ā
Ā Ā Ā Ā print("Before swapping:")Ā Ā Ā Ā # print list before swapping the nodesĀ Ā Ā Ā list.printList()Ā Ā Ā Ā print()Ā
Ā Ā Ā Ā # function call for swapping Kth nodesĀ Ā Ā Ā list.swapKthNodes(1)Ā
Ā Ā Ā Ā print("After swapping nodes for k = 1:")Ā Ā Ā Ā # print list after swapping the nodesĀ Ā Ā Ā list.printList()Ā Ā Ā Ā print()Ā
Ā Ā Ā Ā list.swapKthNodes(2)Ā Ā Ā Ā print("After swapping nodes for k = 2:")Ā Ā Ā Ā list.printList()Ā Ā Ā Ā print()Ā
Ā Ā Ā Ā list.swapKthNodes(3)Ā Ā Ā Ā print("After swapping nodes for k = 3 (middle):")Ā Ā Ā Ā list.printList()Ā Ā Ā Ā print()Ā
# This code is contributed by Tapesh(tapeshuda420) |
C#
// C# program to swap Kth node from beginning with the Kth// node from the end without using temporary node and without// swapping the datausing System;Ā
public class GFG {Ā
Ā Ā Ā Ā // class NodeĀ Ā Ā Ā class Node {Ā Ā Ā Ā Ā Ā Ā Ā public int data;Ā Ā Ā Ā Ā Ā Ā Ā public Node first, next;Ā Ā Ā Ā Ā Ā Ā Ā public Node(int data)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.data = data;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā first = null;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā next = null;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // head pointer for pointing to start of the linked listĀ Ā Ā Ā // last pointer for pointing to last node of the linkedĀ Ā Ā Ā // listĀ Ā Ā Ā Node head = null, last = null;Ā
Ā Ā Ā Ā // function for inserting new node at the end of theĀ Ā Ā Ā // list using last pointerĀ Ā Ā Ā void AddLast(int data)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Node temp = new Node(data);Ā Ā Ā Ā Ā Ā Ā Ā if (head == null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā head = temp;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā last = temp;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā last.next = temp;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp.first = last;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā last = temp;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // function for printing the doubly linked listĀ Ā Ā Ā void printList()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Node p = head;Ā Ā Ā Ā Ā Ā Ā Ā while (p != null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(p.data + "<->");Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p = p.next;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Console.Write("null");Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine();Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // function for swapping Kth node from beginning withĀ Ā Ā Ā // Kth node from the endĀ Ā Ā Ā void swapKthNodes(int k)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int count = 1;Ā Ā Ā Ā Ā Ā Ā Ā Node p = head, q = last;Ā Ā Ā Ā Ā Ā Ā Ā // case 1: to swap the start and end nodes case 1Ā Ā Ā Ā Ā Ā Ā Ā // figureĀ Ā Ā Ā Ā Ā Ā Ā if (k == 1) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first = q.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = p.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = q;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // change these links to null to the breakĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // circular linkĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = null;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first = null;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā head = q;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā last = p;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (p != null && q != null && count < k) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p = p.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q = q.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // case 3: if the nodes to be swapped are theĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // middle nodes given in the figureĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (p.next == q) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p.next.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = p.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first = q.next = p.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.next.first = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first.next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = q;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first = q;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // case 2: other than middle nodes given in caseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // 2 figureĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = q.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = q.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = p.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first = q.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first = p.next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.next.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = q;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = q;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.next.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā static public void Main()Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā GFG list = new GFG();Ā
Ā Ā Ā Ā Ā Ā Ā Ā // function calls for insertingĀ Ā Ā Ā Ā Ā Ā Ā // at the end of the listĀ Ā Ā Ā Ā Ā Ā Ā list.AddLast(10);Ā Ā Ā Ā Ā Ā Ā Ā list.AddLast(20);Ā Ā Ā Ā Ā Ā Ā Ā list.AddLast(30);Ā Ā Ā Ā Ā Ā Ā Ā list.AddLast(40);Ā Ā Ā Ā Ā Ā Ā Ā list.AddLast(50);Ā Ā Ā Ā Ā Ā Ā Ā list.AddLast(60);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("Before swapping:");Ā Ā Ā Ā Ā Ā Ā Ā // print list before swapping the nodesĀ Ā Ā Ā Ā Ā Ā Ā list.printList();Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine();Ā
Ā Ā Ā Ā Ā Ā Ā Ā // function call for swapping Kth nodesĀ Ā Ā Ā Ā Ā Ā Ā list.swapKthNodes(1);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "After swapping nodes for k = 1:");Ā Ā Ā Ā Ā Ā Ā Ā // print list after swapping the nodesĀ Ā Ā Ā Ā Ā Ā Ā list.printList();Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine();Ā
Ā Ā Ā Ā Ā Ā Ā Ā list.swapKthNodes(2);Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "After swapping nodes for k = 2:");Ā Ā Ā Ā Ā Ā Ā Ā list.printList();Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine();Ā
Ā Ā Ā Ā Ā Ā Ā Ā list.swapKthNodes(3);Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "After swapping nodes for k = 3 (middle):");Ā Ā Ā Ā Ā Ā Ā Ā list.printList();Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine();Ā Ā Ā Ā }}Ā
// This code is contributed by lokesh (lokeshmvs21). |
Javascript
// Javascript codeclass Node {Ā Ā Ā Ā constructor(data) {Ā Ā Ā Ā Ā Ā Ā Ā this.data = data;Ā Ā Ā Ā Ā Ā Ā Ā this.first = null;Ā Ā Ā Ā Ā Ā Ā Ā this.next = null;Ā Ā Ā Ā }}Ā
// function for inserting new node at the// end of the list using last pointerfunction AddLast(data, head, last) {Ā Ā Ā Ā let temp = new Node(data);Ā Ā Ā Ā if (!head) {Ā Ā Ā Ā Ā Ā Ā Ā head = temp;Ā Ā Ā Ā Ā Ā Ā Ā last = temp;Ā Ā Ā Ā }Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā last.next = temp;Ā Ā Ā Ā Ā Ā Ā Ā temp.first = last;Ā Ā Ā Ā Ā Ā Ā Ā last = temp;Ā Ā Ā Ā }Ā Ā Ā Ā return [head, last];}Ā
// function for printing the doubly linked listfunction printList(head) {Ā Ā Ā Ā let p = head;Ā Ā Ā Ā while (p) {Ā Ā Ā Ā Ā Ā Ā Ā console.log(`${p.data}<->`);Ā Ā Ā Ā Ā Ā Ā Ā p = p.next;Ā Ā Ā Ā }Ā Ā Ā Ā console.log("NULL");}Ā
// function for swapping Kth node from// beginning with Kth node from the endfunction swapKthNodes(head, last, k) {Ā Ā Ā Ā let count = 1;Ā Ā Ā Ā let p = head, q = last;Ā Ā Ā Ā // case 1: to swap the start and end nodesĀ Ā Ā Ā // case 1 figureĀ Ā Ā Ā if (k == 1) {Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = p;Ā Ā Ā Ā Ā Ā Ā Ā p.first = q.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā q.next = p.next;Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = q;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // change these links to NULL to the break circularĀ Ā Ā Ā Ā Ā Ā Ā // linkĀ Ā Ā Ā Ā Ā Ā Ā p.next = null;Ā Ā Ā Ā Ā Ā Ā Ā q.first = null;Ā
Ā Ā Ā Ā Ā Ā Ā Ā head = q;Ā Ā Ā Ā Ā Ā Ā Ā last = p;Ā Ā Ā Ā Ā Ā Ā Ā return [head, last];Ā Ā Ā Ā }Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā while (p != null && q != null && count < k) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p = p.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q = q.first;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // case 3: if the nodes to be swapped are the middleĀ Ā Ā Ā Ā Ā Ā Ā // nodes given in the figureĀ Ā Ā Ā Ā Ā Ā Ā if (p.next == q) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p.next.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = p.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first = q.next = p.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.next.first = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first.next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = q;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first = q;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā // case 2: other than middle nodesĀ Ā Ā Ā Ā Ā Ā Ā // given in case 2 figureĀ Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = q.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = q.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = p.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first = q.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first = p.next;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.first;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.first;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next = q.next.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.first.next = q;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā q.next.first = q;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next = p.next.next;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.first.next = p;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā p.next.first = p;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā return [head, last];}Ā
// Driver function// head pointer for pointing to start of the linked list// last pointer for pointing to last node of the linked// listlet head = null, last = null;Ā
// function calls for inserting// at the end of the list[head, last] = AddLast(10, head, last);[head, last] = AddLast(20, head, last);[head, last] = AddLast(30, head, last);[head, last] = AddLast(40, head, last);[head, last] = AddLast(50, head, last);[head, last] = AddLast(60, head, last);Ā
console.log("Before swapping:");// print list before swapping the nodesprintList(head);console.log();Ā
// function call for swapping Kth nodes[head, last] = swapKthNodes(head, last, 1);Ā
console.log("After swapping nodes for k = 1:");// print list after swapping the nodesprintList(head);console.log();Ā
[head, last] = swapKthNodes(head, last, 2);console.log("After swapping nodes for k = 2:");printList(head);console.log();Ā
[head, last] = swapKthNodes(head, last, 3);console.log("After swapping nodes for k = 3 (middle):");printList(head);console.log();Ā
// This code is contributed by akashish__ |
Before swapping: 10<->20<->30<->40<->50<->60<->NULL After swapping nodes for k = 1: 60<->20<->30<->40<->50<->10<->NULL After swapping nodes for k = 2: 60<->50<->30<->40<->20<->10<->NULL After swapping nodes for k = 3 (middle): 60<->50<->40<->30<->20<->10<->NULL
Time complexity: O(N) where N is number of nodes in linked list
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



