Swap Nodes in Pairs --成对交换链表节点(重重重)
来源:互联网 发布:网络棋牌机器人规律 编辑:程序博客网 时间:2024/05/21 18:17
题目:
链接
解答:
对链表的考察 算是比较经典的一道题目
循环取出未处理的链表的头两个节点,交换位置,链接到已处理节点的尾部。
这里要注意的是last节点,cur节点 nextcur节点的更新。
在纸上多画画,就可以清楚了。
这道题需要再做一遍,加深印象。
代码:
class Solution {public:ListNode *swapPairs(ListNode *head) {ListNode *cur = head;ListNode *nextcur;ListNode *last;if (head == NULL)return head;if (head->next == NULL)return head;cur = head;head = head->next;nextcur = head->next;head->next = cur;cur->next = nextcur;cur = nextcur;last = head->next;while (1){if (cur == NULL)return head;if (cur->next == NULL)return head;nextcur = cur->next->next;cur->next->next = cur;last->next = cur->next;last = cur;cur->next = nextcur;cur = nextcur;}}};
0 0
- Swap Nodes in Pairs --成对交换链表节点(重重重)
- LeetCode24 Swap Nodes in Pairs 成对交换链表节点
- [LeetCode] Swap Nodes in Pairs(成对交换节点)
- Swap Nodes in Pairs 成对交换结点
- [LeetCode]Swap Nodes in Pairs 成对交换
- Swap Nodes in Pairs 链表交换相邻的节点
- 【leetcode】24. Swap Nodes in Pairs - 成对交换链表元素
- 451.Swap Nodes in Pairs-两两交换链表中的节点(容易题)
- [LeetCode]Swap Nodes in Pairs 交换节点
- [leetcode 24]Swap Nodes in Pairs-----成对翻转链表中的节点
- LeetCode 24. Swap Nodes in Pairs(交换节点)
- leetcode_24. Swap Nodes in Pairs 单链表中成对交换结点位置
- LeetCode Swap Nodes in Pairs交换链表中的两个节点
- Leetcode Swap Nodes in Pairs 交换链表的节点对
- Leetcode #24. Swap Nodes in Pairs 链表节点交换 解题报告
- LeetCode OJ 之 Swap Nodes in Pairs (交换成对相邻结点的值)
- leetcode解题之24. Swap Nodes in Pairs java 版(成对翻转链表)
- Swap Nodes in Pairs 链表两两交换节点@LeetCode
- 入睡时分,夜已深,潦草文笔记下博客开篇之日
- Dynamics CRM 2013 停用和激活按钮的显示与隐藏
- HDU-1385 Minimum Transport Cost
- linux下mysql的root密码忘记解决方
- HDU 4944 FSF’s game(数论+递推)
- Swap Nodes in Pairs --成对交换链表节点(重重重)
- apache中ab工具
- hdu 1006 Tick and Tick 模拟时钟注意时间连续性!
- 字符编码与字库开发基本原理学习(一)
- SQL data reader reading data performance test
- hdu 4937 Lucky Number 多校第七场
- sql防止注入攻击的方法
- HDU 4939 Stupid Tower Defense (DP)
- win7+ubuntu双系统安装方法