lintcode-Swap Nodes in Pairs -451
来源:互联网 发布:电脑怎么解除网络限制 编辑:程序博客网 时间:2024/06/05 15:25
Given a linked list, swap every two adjacent nodes and return its head.
样例
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.
/** * 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||!head->next) return head; ListNode *ptr=head; while(ptr){ if(!ptr->next) break; swap(ptr->next->val,ptr->val); ptr=ptr->next->next; } return head; }};
0 0
- lintcode-Swap Nodes in Pairs -451
- [Lintcode]Swap Nodes in Pairs
- Swap Nodes in Pairs(LintCode)
- #451 Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes In Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- 第六周--数据结构之自建算法库之表达式求值(用栈结构)
- LogBean
- Oracle pl/sql编程 3---plsql为常量与变量赋值
- [BZOJ 2301][HAOI2011] Problem b 莫比乌斯反演
- 自定义UISwitch颜色
- lintcode-Swap Nodes in Pairs -451
- 驱动调试
- 使用sizeof计算类的大小
- poj 2758(后缀数组)
- JAVA 文件编码转换
- 文章标题
- GlobalInterceptor
- bzoj3890【Usaco2015 Jan】Meeting Time
- Using C-style cast. Use reinterpret_cast<int*>(…) instead [readability/casting] [4]