LeetCode: Swap Nodes in Pairs 解题报告
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.

SOLUTION 1:
递归解法:
1. 先翻转后面的链表,得到新的Next.
2. 翻转当前的2个节点。
3. 返回新的头部。
// Solution 1: the recursion version.
public ListNode swapPairs1(ListNode head) {
if (head == null) {
return null;
} return rec(head);
} public ListNode rec(ListNode head) {
if (head == null || head.next == null) {
return head;
} ListNode next = head.next.next; // 翻转后面的链表
next = rec(next); // store the new head.
ListNode tmp = head.next; // reverse the two nodes.
head.next = next;
tmp.next = head; return tmp;
}
SOLUTION 2:
迭代解法:
1. 使用Dummy node保存头节点前一个节点
2. 记录翻转区域的Pre(上一个节点),记录翻转区域的next,或是tail。
3. 翻转特定区域,并不断前移。
有2种写法,后面一种写法稍微简单一点,记录的是翻转区域的下一个节点。
// Solution 2: the iteration version.
public ListNode swapPairs(ListNode head) {
// 如果小于2个元素,不需要任何操作
if (head == null || head.next == null) {
return head;
} ListNode dummy = new ListNode(0);
dummy.next = head; // The node before the reverse area;
ListNode pre = dummy; while (pre.next != null && pre.next.next != null) {
// The last node of the reverse area;
ListNode tail = pre.next.next; ListNode tmp = pre.next;
pre.next = tail; ListNode next = tail.next;
tail.next = tmp;
tmp.next = next; // move forward the pre node.
pre = tmp;
} return dummy.next;
}
// Solution 3: the iteration version.
public ListNode swapPairs3(ListNode head) {
// 如果小于2个元素,不需要任何操作
if (head == null || head.next == null) {
return head;
} ListNode dummy = new ListNode(0);
dummy.next = head; // The node before the reverse area;
ListNode pre = dummy; while (pre.next != null && pre.next.next != null) {
ListNode next = pre.next.next.next; ListNode tmp = pre.next;
pre.next = pre.next.next;
pre.next.next = tmp; tmp.next = next; // move forward the pre node.
pre = tmp;
} return dummy.next;
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/SwapPairs3.java
LeetCode: Swap Nodes in Pairs 解题报告的更多相关文章
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- [LeetCode]Swap Nodes in Pairs题解
Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- leetcode—Swap Nodes in Pairs
1.题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [LeetCode]Swap Nodes in Pairs 成对交换
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...
- LeetCode: Reverse Nodes in k-Group 解题报告
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
随机推荐
- 【原创】浅析密码学在互联网支付中的应用|RSA,Hash,AES,DES,3DES,SHA1,SHA256,MD5,SSL,Private Key,Public Key
一)概述 什么是互联网支付? 当支付遇到互联网,一场革命自然不可避免.成为现实的是传统的现金支付已经“退居二线”,各种在线支付方式成为人们日常消费的主要支付方式.银行推出的网银以及第三方支付公司推出的 ...
- 〖Linux〗VirtualBox修改虚拟电脑硬盘(vdi)空间大小
1. 查看需要修改的虚拟硬盘: [scue@Link:tftpserver]$ vboxmanage list hdds UUID: 79d65850--40c3-a8e7-715b199d1673 ...
- 〖Android〗酷派手机固件.cpb文件的分解程序
/* * ===================================================================================== * * Filen ...
- 同域名不同端口应用共享sessionid问题解决办法
相同域名共享sessionid 如 www.abc.com www.abc.com/test www.abc.com:8080 www.abc.com:8989 这些地址由于是在相同的域名下,所以共享 ...
- Spring(十二)使用Spring的xml文件配置方式实现AOP
配置文件与注解方式的有非常大不同,多了非常多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"? & ...
- PHP一些优先级的问题
直接看代码 <?php echo '1'.print(2)+3,"\n"; 不错,就是这么简单,但是很少有人能正确回答 我们执行一下 [root@localhost test ...
- Object-c中block需要注意的几点问题
1. Block定义 1) 说明: a. Block是OC中的一种数据类型,在iOS开发中被广泛使用 b. ^是Block的特有标记 c. Block的实现代码包含在{}之间 d. 大多情况下,以内联 ...
- ios block常见的错误(一)
Block是OC中的一种数据类型,在iOS开发中被广泛使用. 在实际的使用和面试过程中,经常发现同事或面试者对block了解得不够透彻,本博文将列举常见block错误来对其加深理解. 示例代码1: ; ...
- ubuntu 重新挂载home
vmware虚拟机下操作: 1 在Vmware中创建新硬盘. 2 启动Ubuntu 在终端输入:sudo fdisk -l ,可以看到 -------------------------------- ...
- zookeeper与kafka安装部署及java环境搭建(发布订阅模式)
1. ZooKeeper安装部署 本文在一台机器上模拟3个zk server的集群安装. 1.1. 创建目录.解压 cd /usr/ #创建项目目录 mkdir zookeeper cd zookee ...