[Leetcode 24 链表]Swap Nodes in Pairs
来源:互联网 发布:蚁群算法详解 编辑:程序博客网 时间:2024/05/13 22:04
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.
加个表头节点,各种清晰。
class Solution { public: ListNode *swapPairs(ListNode *head) { ListNode nodehead(0); ListNode *pmove = &nodehead; nodehead.next = head; //if have only one node,won't go into while ListNode *first = head; ListNode *second = head?head->next:NULL; //head may be NULL while(first && second) { first->next = second->next; second->next = first; pmove->next = second; pmove = first; first = first?first->next:NULL; second = first?first->next:NULL; } return nodehead.next; }};
0 0
- [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---swap-nodes-in-pairs---链表
- LeetCode(24)Swap Nodes in Pairs
- [LeetCode 24] Swap Nodes in Pairs
- [leetcode 24]Swap Nodes in Pairs
- LeetCode | #24 Swap Nodes in Pairs
- leetcode 24 Swap Nodes in Pairs
- 【leetcode】【单链表】【24】Swap Nodes in Pairs
- Leetcode 24 Swap Nodes in Pairs
- Swap Nodes in Pairs - LeetCode 24
- LeetCode 24 - Swap Nodes in Pairs
- leetcode 24 -- Swap Nodes in Pairs
- LeetCode 24:Swap Nodes in Pairs
- leetcode-24Swap Nodes in Pairs
- java程序打包成为可运行的exe文件
- 在C++中嵌入V8
- android v7兼容包RecyclerView的使用(二)
- 分布式编译IncredBuild
- COM接口Hook的用法
- [Leetcode 24 链表]Swap Nodes in Pairs
- DBCP,CP30及proxool连接池在spring+hibernate中的配置
- hdu 1223 最小生成树 (MST)
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- js写的留言板。
- 一个让老程序员都汗颜的应届生——传智播客iOS二期学员常小帅自述
- Notepad++ 快捷键 大全
- 让图片DIV竖向滑动的JavaScript特效代码
- 数组指针与指针数组