算法设计与分析(17)-- Swap Nodes in Pairs(难度:Medium)
来源:互联网 发布:网络大电影市千万票房 编辑:程序博客网 时间:2024/05/21 11:15
算法设计与分析(17)
题目:Swap Nodes in Pairs
问题描述:
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
算法思路
问题是把一个链表的每两个节点进行交换位置。一个链表节点交换位置,按题目的要求,不能通过更改节点里的取值,实际上只需要更改节点中的next指针。所以这里的算法比较简单,我们只需要额外引入两个指针ListNode *first, *second
,就可以完成任务。
(1)首先,若头指针为空直接返回NULL。若不为空,first指向头部第一个节点,接着若head->next不为空,用second指向第二个节点。然后为了交换first和second的位置,先使head指向第二个节点,再改变first和second的next取值:
ListNode *first, *second;if (head == NULL) return head;first = head;if (head->next != NULL){ second = first->next; head = second; first->next = second->next; second->next = first;}
(2)注意这个时候,由于已经交换位置,实际上first才是second的下一个节点。所以接下来我们判断first->next与first->next->next是否为空,只有当两个都不为空时,我们才对他们两个节点进行交换。
交换过程也比较简单:先使用ListNode *temp
取值为first,在将first->next和first->next->next重新用first和second指向他们。接着就可以通过改变他们的next取值来更变位置。
while (first->next != NULL && first->next->next != NULL){ ListNode *temp = first; first = first->next; second = first->next; temp->next = second; first->next = second->next; second->next = first;}
(3)最后返回头指针。
程序运行结果:
实现代码
#include <iostream>#include <vector>using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};ListNode* swapPairs(ListNode* head) { ListNode *first, *second; if (head == NULL) return head; first = head; if (head->next != NULL) { second = first->next; head = second; first->next = second->next; second->next = first; } while (first->next != NULL && first->next->next != NULL) { ListNode *temp = first; first = first->next; second = first->next; temp->next = second; first->next = second->next; second->next = first; } return head;}int main(){ ListNode a1(1), a2(2), a3(3), a4(4); a1.next = &a2; a2.next = &a3; a3.next = &a4; ListNode *head = &a1; swapPairs(head); while (head != NULL) { cout << head->val << endl; head = head->next; } return 0;}
- 算法设计与分析(17)-- Swap Nodes in Pairs(难度:Medium)
- 第四周算法分析与设计:Swap Nodes in Pairs
- (算法分析Week11)Swap Nodes in Pairs[Medium]
- LeetCode刷题——Swap Nodes in Pairs(Medium)
- 算法分析与设计——LeetCode Problem.24 Swap Nodes in Pairs
- 算法第五周Swap Nodes in Pairs[medium]
- [Leetcode 24, Medium] Swap Nodes in Pairs
- 算法设计与分析(18)-- Reverse Nodes in k-Group(难度:Hard)
- 【LeetCode算法练习(C++)】Swap Nodes in Pairs
- 算法 Swap Nodes in Pairs
- LeetCode(24) Swap Nodes in Pairs
- Swap Nodes in Pairs(LintCode)
- Swap Nodes in Pairs(easy)
- 24. Swap Nodes in Pairs(6.30%)
- 24. Swap Nodes in Pairs(python)
- Swap Nodes in Pairs (EASY)
- LeetCode (24)Swap Nodes in Pairs
- leetcode Swap Nodes in Pairs 2.10 难度系数2
- U-boot中的宏定义
- Xcode文件突然显示不全只显示一部分
- mongo
- Exyons4412音频驱动读写流程
- 【Visual Studio】VS常用调试技巧——笔记
- 算法设计与分析(17)-- Swap Nodes in Pairs(难度:Medium)
- 判断括号是否匹配
- 乐驾 -- 7
- pyGTK 列表刷新model
- java_try,catch,finally return之间的执行顺序
- 006 音量上下键调节app使用
- 队列
- 硬盘的读写原理
- 进程状态切换