leetcode: Swap Nodes in Pairs (C++)
来源:互联网 发布:pmp十五矩阵 编辑:程序博客网 时间:2024/05/21 01:43
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.
基本实现思路是设置三个滑动指针 t, p, q 对 ListNode 进行 swap 操作。
<span style="color:#333333;">/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *swapPairs(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *t = new ListNode(0); </span><span style="color:#009900;">//设置相邻的 3 个指针 t, p, q</span><span style="color:#333333;"> t->next = head; ListNode *p = head; ListNode *q = head->next; head = q; while(1) { t->next = q; p->next = q->next; q->next = p; if(p->next == NULL || p->next->next == NULL) break; t = p; p = p->next; q = p->next; } return head; }};</span>
0 0
- leetcode: Swap Nodes in Pairs (C++)
- 【LeetCode-24】Swap Nodes in Pairs(C++)
- 【C++】【LeetCode】24. Swap Nodes in Pairs
- Leetcode c语言-Swap Nodes in Pairs
- LeetCode:Swap Nodes in Pairs
- LeetCode: Swap Nodes in Pairs
- [Leetcode] Swap Nodes in Pairs
- LeetCode : Swap Nodes in Pairs
- [LeetCode] Swap Nodes in Pairs
- Leetcode: Swap nodes in pairs
- [LeetCode]Swap Nodes in Pairs
- [LeetCode]Swap Nodes in Pairs
- LeetCode-Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- LeetCode | Swap Nodes in Pairs
- 【leetcode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- 托管代码和非托管代码
- 实例学习gcc+gdb+make
- 管理老板的方法
- 改变世界的17个等式
- DM9000+stm32F051调试过程中出现的问题
- leetcode: Swap Nodes in Pairs (C++)
- PHP在linux读取word文档
- 360必须添加语句
- 滤波器设计指标
- ORA-00054:资源正忙,但指定以NOWAIT方式获取资源,或者超时失效的问题
- 数字信号处理中各种频率关系
- Android4.4支持使用DevTools对原生Android应用程序WebViews内容进行调试
- android坐标
- 学习FPGA的网站推荐