[Lintcode]Swap Nodes in Pairs
来源:互联网 发布:cydia红包软件源 编辑:程序博客网 时间:2024/06/07 01:23
Given a linked list, swap every two adjacent nodes and return its head.
Example
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { /** * @param head a ListNode * @return a ListNode */ public ListNode swapPairs(ListNode head) { if(head == null) return null; ListNode dummy = new ListNode(0); dummy.next = head; ListNode prev = dummy; while(prev.next != null && prev.next.next != null) { ListNode cur = prev.next; ListNode tmp = prev.next.next.next; prev.next = prev.next.next; prev.next.next = cur; cur.next = tmp; prev = prev.next.next; } return dummy.next; }}
0 0
- [Lintcode]Swap Nodes in Pairs
- lintcode-Swap Nodes in Pairs -451
- Swap Nodes in Pairs(LintCode)
- 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
- javascript第六天
- mybatis的增删查改(CRUD)
- spring中如何定义定时任务
- PHP--set_include_path和get_include_path用法详解
- POJ 2299 Ultra-QuickSort
- [Lintcode]Swap Nodes in Pairs
- 【C++】强制类型转换运算符
- Github的最简化使用方案
- iOS学习笔记32-iCloud入门
- Error:Execution failed for task ':app:buildInfoDebugLoader'. > Exception while doing past iteration
- 使用 matlab 数字图像处理(一)—— 归一化直方图
- 射线及其碰撞检测 [代码清单6-7]
- Android开源之BaseRecyclerViewAdapterHelper(持续更新!)
- 第七周总结及Boostrap框架demo