C++ 給定連結串列的成對交換元素


要解決一個需要我們交換連結串列中成對節點並列印的問題,例如

Input : 1->2->3->4->5->6->NULL

Output : 2->1->4->3->6->5->NULL

Input : 1->2->3->4->5->NULL

Output : 2->1->4->3->5->NULL

Input : 1->NULL

Output : 1->NULL

有兩種方法可以解決這個問題,兩種方法的時間複雜度都是 O(N),其中 N 是我們提供的連結串列的大小,所以現在我們將探討這兩種方法

迭代方法

在這種方法中,我們將遍歷連結串列元素,併成對交換它們,直到它們到達 NULL。

示例

#include <bits/stdc++.h>
using namespace std;
class Node { // node of our list
public:
    int data;
    Node* next;
};
void swapPairwise(Node* head){
    Node* temp = head;
    while (temp != NULL && temp->next != NULL) { // for pairwise swap we need to have 2 nodes hence we are checking
        swap(temp->data,
            temp->next->data); // swapping the data
        temp = temp->next->next; // going to the next pair
    }
}
void push(Node** head_ref, int new_data){ // function to push our data in list
    Node* new_node = new Node(); // creating new node
    new_node->data = new_data;
    new_node->next = (*head_ref); // head is pushed inwards
    (*head_ref) = new_node; // our new node becomes our head
}
void printList(Node* node){ // utility function to print the given linked list
    while (node != NULL) {
       cout << node->data << " ";
       node = node->next;
    }
}
int main(){
    Node* head = NULL;
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
    cout << "Linked list before\n";
    printList(head);
    swapPairwise(head);
    cout << "\nLinked list after\n";
    printList(head);
    return 0;
}

輸出

Linked list before
1 2 3 4 5
Linked list after
2 1 4 3 5

我們將在下面的方法中使用相同的公式,但我們將透過遞迴進行迭代。

遞迴方法

在這種方法中,我們使用遞迴實現了相同的邏輯。

示例

#include <bits/stdc++.h>
using namespace std;
class Node { // node of our list
public:
    int data;
    Node* next;
};
void swapPairwise(struct Node* head){
    if (head != NULL && head->next != NULL) { // same condition as our iterative
        swap(head->data, head->next->data); // swapping data
        swapPairwise(head->next->next); // moving to the next pair
    }
    return; // else return
}
void push(Node** head_ref, int new_data){ // function to push our data in list
    Node* new_node = new Node(); // creating new node
    new_node->data = new_data;
    new_node->next = (*head_ref); // head is pushed inwards
    (*head_ref) = new_node; // our new node becomes our head
}
void printList(Node* node){ // utility function to print the given linked list
    while (node != NULL) {
        cout << node->data << " ";
        node = node->next;
    }
}
int main(){
    Node* head = NULL;
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
    cout << "Linked list before\n";
    printList(head);
    swapPairwise(head);
    cout << "\nLinked list after\n";
    printList(head);
    return 0;
}

輸出

Linked list before
1 2 3 4 5
Linked list after
2 1 4 3 5

上述程式碼的解釋

在這種方法中,我們成對地遍歷連結串列。現在,當我們到達一對時,我們交換它們的資料並移動到下一對,這就是我們的程式在兩種方法中都進行的方式。

結論

在本教程中,我們使用遞迴和迭代解決了給定連結串列的成對交換元素問題。我們還學習了此問題的 C++ 程式以及我們解決此問題的完整方法(常規)。我們可以在其他語言(如 C、Java、Python 和其他語言)中編寫相同的程式。我們希望您覺得本教程有所幫助。

更新於: 2021-11-25

322 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告