24. Swap Nodes in Pairs
来源:互联网 发布:java pdf嵌入页面 编辑:程序博客网 时间:2024/05/03 07:27
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) { if(!head ||head->next == NULL) return head; ListNode dum(0), *first, *second; ListNode *temp = &dum; dum.next = head; while(temp->next != NULL && temp->next->next !=NULL) { first = temp->next; second = temp->next->next; first->next = second->next; temp->next = second; temp->next->next = first; temp = temp->next->next; } return dum.next; }};
0 0
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24.Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- OJ_1176,神奇的开学发作业本题
- 交易咖---死扛死扛,扛出辉煌
- HDU - 1249 三角形(平面划分)
- 嵌入式学习心得(六)
- Widgte的小bug
- 24. Swap Nodes in Pairs
- 算法随笔(扩欧)
- 怎么查看VS2010所支持的C++11标准/C99标准的头文件
- 浅析C++中的多态
- O1,O2,O3优化了什么,链接做了什么;静态链接-static
- 指针学习笔记(下)
- Generate DB2 table definition COPYBOOK
- 今日学习
- 读书笔记——《你必须知道的495个C语言问题》2