LeetCode --- Swap Nodes in Pairs
来源:互联网 发布:淘宝论文的代写哪家好 编辑:程序博客网 时间:2024/05/24 06:50
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.
My Submitted Code
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {private:public: ListNode *swapPairs(ListNode *head) { if(!head || !head->next){ return head; } ListNode *prev; ListNode *p1; ListNode *p2; p1=head; prev=head; do{ p2=p1->next; p1->next=p2->next; p2->next=p1; if(prev!=head) prev->next=p2; else head=p2; prev=p1; p1=p1->next; }while(p1 && p1->next); return head; }};
0 0
- 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
- 【LeetCode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- Leetcode Swap Nodes in Pairs
- LeetCode Swap Nodes in Pairs
- BAT 批处理脚本 教程
- 关于创业的一些边缘思考
- UITableView/UIScrollView内容视图下移
- HTTP协议详解
- 谈中国人的信仰问题
- LeetCode --- Swap Nodes in Pairs
- line
- 高斯滤波
- 会员卡管理系统技术解析(九)会员卡领卡记录查询
- 无废话WCF入门教程一[什么是WCF]
- ubuntu 编译SDK报appt 问题,32
- C# AccessHelper 完整示例代码
- 发现一个非常好的ping工具
- 2015百度之星初赛(1)problem1002