Swap Nodes in Pairs
2016-06-07 07:54
387 查看
又是基本功,链表的基本操作。
注意犯错的地方
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
while (head != null && head.next != null) {
ListNode temp = head.next.next;
prev.next = head.next;
head.next.next = head;
////////
head.next = temp;
////////
prev = head;
head = temp;
}
return dummy.next;
}
}
注意犯错的地方
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
while (head != null && head.next != null) {
ListNode temp = head.next.next;
prev.next = head.next;
head.next.next = head;
////////
head.next = temp;
////////
prev = head;
head = temp;
}
return dummy.next;
}
}
相关文章推荐
- Print nodes in a given range of BST
- NodeJS 不可逆加密与密码密文保存
- leecode_222 Count Complete Tree Nodes
- DrawNode绘制几何图形
- node.js 和 express 框架学习笔记(2)
- node js 获取ip
- javascript中children和childNodes的区别
- Study NodeJS《NPM常用命令》
- 【LeetCode】011 Swap Nodes in Pairs 两两换位
- 《Nodejs开发加密货币》之十七:共识机制,可编程的利益转移规则
- ambari回滚 禁止namenode ha
- Folk博客程序——可以运行于node环境和asp环境的博客系统
- node定时任务
- inode与super block
- BAE log服务的配置(nodejs)
- 6个讨喜的 ES6 小技巧
- Node学习笔记(三):基于socket.io web版你画我猜(二)
- 【leetcode】25. Reverse Nodes in k-Group
- Node.js的Web模板引擎ejs的入门使用教程
- 关于nodejs邮件群发的Nodemailer