CODE 108: Swap Nodes in Pairs
来源:互联网 发布:php json encode 在线 编辑:程序博客网 时间:2024/05/21 14:50
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.
public ListNode swapPairs(ListNode head) {// Start typing your Java solution below// DO NOT write main() functionListNode newHead = new ListNode(0);ListNode tmpHead = newHead;ListNode tmp = head;while (null != tmp && null != tmp.next) {ListNode first = tmp;ListNode second = tmp.next;tmp = second.next;tmpHead.next = second;tmpHead.next.next = first;tmpHead = tmpHead.next.next;}if (null != tmp) {tmpHead.next = tmp;tmpHead = tmpHead.next;}tmpHead.next = null;return newHead.next;}
- CODE 108: Swap Nodes in Pairs
- [leet code] Swap Nodes in Pairs
- leet code -- 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
- Swap Nodes in Pairs
- Android 3.1新的广播机制
- C++之对象数组和对象指针
- 华为面试题解析 - 01
- Quartz 2D编程指南(4) - 颜色和颜色空间
- 网站优化与推广—robots文件处理
- CODE 108: Swap Nodes in Pairs
- ubuntu下安装codeblocks12.11
- ExpandableListView长按事件完美解决
- 做最好的进销存管理系统--一桶金进销存软件www.jxc123.info
- 验证码封装类
- 网站优化与推广—ping服务
- 隐藏在QRCode二维码背后的秘密
- linux常见面试题
- Const,Const函数,Const变量,函数后面的Const (zz)