📌  相关文章
📜  在不修改数据的情况下交换双向链表中的给定节点

📅  最后修改于: 2022-05-13 01:57:59.092000             🧑  作者: Mango

在不修改数据的情况下交换双向链表中的给定节点

给定一个具有所有唯一元素和两个键XY的双向链表,任务是仅通过更改链接来交换两个给定键的节点。

注意:可以认为 X 和 Y 始终存在于列表中。

例子

方法:这个问题可以通过遍历指向具有值 X 和 Y 的节点的指针并交换它们来解决。

请按照以下步骤操作:

  • 在给定的双向链表中搜索XY。
  • 搜索完成后,通过将X的前一个相邻指针作为Y的前一个相邻指针,将X的下一个相邻指针作为Y的下一个相邻指针来交换节点,反之亦然。

下面是上述方法的实现:

C++
// C++ code to implement the above approach
 
#include 
using namespace std;
 
// Link list Node Class
class Node {
public:
    int data;
    Node* prev;
    Node* next;
 
    // Constructor function
    Node(int data)
    {
        this->data = data;
        this->prev = NULL;
        this->next = NULL;
    }
};
 
// Function to print linked list
void print(Node* head)
{
    Node* temp = head;
 
    // Iterate until node is NOT NULL
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}
 
// Function to push a node in DLL
void push(Node*& head, Node*& tail,
          int item)
{
 
    // DLL is empty
    if (tail == NULL) {
 
        Node* temp = new Node(item);
        tail = temp;
        head = temp;
    }
 
    // DLL is not empty
    else {
        Node* temp = new Node(item);
        tail->next = temp;
        temp->prev = tail;
        tail = temp;
    }
}
 
// Function to find the nodes
// which have to be swapped
pair find(Node*& head,
                        int x, int y)
{
    Node* N1 = NULL;
    Node* N2 = NULL;
    Node* temp = head;
 
    // Traversing the list
    while (temp != NULL) {
        if (temp->data == x)
            N1 = temp;
        else if (temp->data == y)
            N2 = temp;
        temp = temp->next;
    }
    return make_pair(N1, N2);
}
 
// Function to swap the nodes
// consisting of x and y
void swap(Node*& head, Node*& tail,
          int x, int y)
{
 
    // Edge Cases
    if (head == NULL || head->next == NULL
        || x == y)
        return;
 
    // Finding the Nodes
    pair p = find(head, x, y);
 
    Node* Node1 = p.first;
    Node* Node2 = p.second;
 
    if (Node1 == head)
        head = Node2;
    else if (Node2 == head)
        head = Node1;
    if (Node1 == tail)
        tail = Node2;
    else if (Node2 == tail)
        tail = Node1;
 
    // Swapping Node1 and Node2
    Node* temp;
    temp = Node1->next;
    Node1->next = Node2->next;
    Node2->next = temp;
 
    if (Node1->next != NULL)
        Node1->next->prev = Node1;
    if (Node2->next != NULL)
        Node2->next->prev = Node2;
 
    temp = Node1->prev;
    Node1->prev = Node2->prev;
    Node2->prev = temp;
 
    if (Node1->prev != NULL)
        Node1->prev->next = Node1;
    if (Node2->prev != NULL)
        Node2->prev->next = Node2;
}
 
// Driver Code
int main()
{
 
    Node* head = NULL;
    Node* tail = NULL;
 
    push(head, tail, 1);
    push(head, tail, 8);
    push(head, tail, 7);
    push(head, tail, 9);
    push(head, tail, 4);
 
    int X = 1, Y = 4;
 
    cout << "Before Swapping: ";
    print(head);
 
    swap(head, tail, X, Y);
    cout << "After Swapping: ";
    print(head);
 
    return 0;
}


Java
// Java code to implement the above approach
import java.util.*;
 
class GFG{
 
// Link list Node Class
static class Node {
 
    int data;
    Node prev;
    Node next;
 
    // Constructor function
    Node(int data)
    {
        this.data = data;
        this.prev = null;
        this.next = null;
    }
};
static class pair
{
    Node first, second;
    public pair(Node first, Node second) 
    {
        this.first = first;
        this.second = second;
    }   
}
 
// Function to print linked list
static void print(Node head)
{
    Node temp = head;
 
    // Iterate until node is NOT null
    while (temp != null) {
        System.out.print(temp.data+ " ");
        temp = temp.next;
    }
    System.out.println();
}
static Node head;
static Node tail;
// Function to push a node in DLL
static void push( int item)
{
 
    // DLL is empty
    if (tail == null) {
 
        Node temp = new Node(item);
        tail = temp;
        head = temp;
    }
 
    // DLL is not empty
    else {
        Node temp = new Node(item);
        tail.next = temp;
        temp.prev = tail;
        tail = temp;
    }
}
 
// Function to find the nodes
// which have to be swapped
static pair find(int x, int y)
{
    Node N1 = null;
    Node N2 = null;
    Node temp = head;
 
    // Traversing the list
    while (temp != null) {
        if (temp.data == x)
            N1 = temp;
        else if (temp.data == y)
            N2 = temp;
        temp = temp.next;
    }
    return new pair(N1, N2);
}
 
// Function to swap the nodes
// consisting of x and y
static void swap( int x, int y)
{
 
    // Edge Cases
    if (head == null || head.next == null
        || x == y)
        return;
 
    // Finding the Nodes
    pair p = find( x, y);
 
    Node Node1 = p.first;
    Node Node2 = p.second;
 
    if (Node1 == head)
        head = Node2;
    else if (Node2 == head)
        head = Node1;
    if (Node1 == tail)
        tail = Node2;
    else if (Node2 == tail)
        tail = Node1;
 
    // Swapping Node1 and Node2
    Node temp;
    temp = Node1.next;
    Node1.next = Node2.next;
    Node2.next = temp;
 
    if (Node1.next != null)
        Node1.next.prev = Node1;
    if (Node2.next != null)
        Node2.next.prev = Node2;
 
    temp = Node1.prev;
    Node1.prev = Node2.prev;
    Node2.prev = temp;
 
    if (Node1.prev != null)
        Node1.prev.next = Node1;
    if (Node2.prev != null)
        Node2.prev.next = Node2;
}
 
// Driver Code
public static void main(String[] args)
{
 
    head = null;
    tail = null;
 
    push( 1);
    push( 8);
    push(7);
    push(9);
    push( 4);
 
    int X = 1, Y = 4;
 
    System.out.print("Before Swapping: ");
    print(head);
 
    swap( X, Y);
    System.out.print("After Swapping: ");
    print(head);
}
}
 
// This code is contributed by shikhasingrajput



输出
Before Swapping: 1 8 7 9 4 
After Swapping: 4 8 7 9 1 

时间复杂度:O(N)
辅助空间:O(1)