给定一个链表,将链表中的元素两两交换
例如,为了解决需要交换链表中存在的成对节点然后打印它的问题
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中的移动语义来通过避免不必要的复制来提高性能。它涵盖了使用std :: Move的实施移动构造函数和任务运算符,并确定了关键方案和陷阱以有效

C语言函数是代码模块化和程序搭建的基础。它们由声明(函数头)和定义(函数体)组成。C语言默认使用值传递参数,但也可使用地址传递修改外部变量。函数可以有返回值或无返回值,返回值类型必须与声明一致。函数命名应清晰易懂,使用驼峰或下划线命名法。遵循单一职责原则,保持函数简洁性,以提高可维护性和可读性。
