Swap Nodes in Pairs
来源:互联网 发布:python ui 编辑:程序博客网 时间:2024/06/14 04:10
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 class SwapNodesInPairs {//acceptedpublic ListNode swapPairs(ListNode head){ListNode dummy = new ListNode(0);dummy.next = head;ListNode current = dummy;while(current.next != null && current.next.next !=null){ListNode first = current.next;ListNode second = current.next.next;first.next = second.next;current.next = second;current.next.next = first;current = current.next.next;}return dummy.next;}}
阅读全文
0 0
- 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
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Ubuntu 14.04下CUDA8.0 + cuDNN v5 安装配置过程
- 用cnavas绘制五角星
- struts2文件上传细节
- Nginx代理功能与负载均衡详解
- 感知机更新规则 python
- Swap Nodes in Pairs
- 剑指offer 面试题3 二维数组中的查找
- PHP程序员要想在北京买房你得狠
- Spring 注入 null
- CodeSmith 7.01破解下载
- 红黑树
- C#中获取计算机当前日期时间等的不同格式
- PHP学习路线图
- 进销项管理专家