給定一個鍊錶,將鍊錶中的元素兩兩交換
例如,為了解決需要交換鍊錶中存在的成對節點然後列印它的問題
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 是我們提供的鍊錶的大小,所以現在我們將探索這兩種方法
< h2>迭代方法我們將在此方法中迭代鍊錶元素,並逐對交換它們,直到它們達到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等語言來寫同樣的程式。我們希望本教學對您有所幫助。
以上是給定一個鍊錶,將鍊錶中的元素兩兩交換的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

C語言數據結構:樹和圖的數據表示與操作樹是一個層次結構的數據結構由節點組成,每個節點包含一個數據元素和指向其子節點的指針二叉樹是一種特殊類型的樹,其中每個節點最多有兩個子節點數據表示structTreeNode{intdata;structTreeNode*left;structTreeNode*right;};操作創建樹遍歷樹(先序、中序、後序)搜索樹插入節點刪除節點圖是一個集合的數據結構,其中的元素是頂點,它們通過邊連接在一起邊可以是帶權或無權的數據表示鄰

文件操作難題的真相:文件打開失敗:權限不足、路徑錯誤、文件被佔用。數據寫入失敗:緩衝區已滿、文件不可寫、磁盤空間不足。其他常見問題:文件遍歷緩慢、文本文件編碼不正確、二進製文件讀取錯誤。

文章討論了在C中有效使用RVALUE參考,以進行移動語義,完美的轉發和資源管理,重點介紹最佳實踐和性能改進。(159個字符)

C 20範圍通過表現力,合成性和效率增強數據操作。它們簡化了複雜的轉換並集成到現有代碼庫中,以提高性能和可維護性。

C語言函數是代碼模塊化和程序搭建的基礎。它們由聲明(函數頭)和定義(函數體)組成。 C語言默認使用值傳遞參數,但也可使用地址傳遞修改外部變量。函數可以有返回值或無返回值,返回值類型必須與聲明一致。函數命名應清晰易懂,使用駝峰或下劃線命名法。遵循單一職責原則,保持函數簡潔性,以提高可維護性和可讀性。

本文討論了使用C中的移動語義來通過避免不必要的複制來提高性能。它涵蓋了使用std :: Move的實施移動構造函數和任務運算符,並確定了關鍵方案和陷阱以有效

本文討論了C中的動態調度,其性能成本和優化策略。它突出了動態調度會影響性能並將其與靜態調度進行比較的場景,強調性能和之間的權衡

C35 的計算本質上是組合數學,代表從 5 個元素中選擇 3 個的組合數,其計算公式為 C53 = 5! / (3! * 2!),可通過循環避免直接計算階乘以提高效率和避免溢出。另外,理解組合的本質和掌握高效的計算方法對於解決概率統計、密碼學、算法設計等領域的許多問題至關重要。
