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 解题报告的更多相关文章

  1. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  2. [LeetCode]Swap Nodes in Pairs题解

    Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  3. [LeetCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  4. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  5. leetcode—Swap Nodes in Pairs

    1.题目描述 Given a linked list, swap every two adjacent nodes and return its head.   For example, Given ...

  6. Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  7. [LeetCode]Swap Nodes in Pairs 成对交换

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  8. [Leetcode] Swap nodes in pairs 成对交换结点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...

  9. 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 ...

随机推荐

  1. MariaDB删除重复记录性能测试

    删除重复记录,只保留id最大的一条记录的性能测试 环境 测试表的id为是唯一的,或是自增的主键. mysql不能直接写循环,只能写在存储过程里. 存储过程usp_batch_insert的参数num_ ...

  2. [C#]记录程序耗时的方法【转发】

    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // H ...

  3. Memcached安装与配置

     memcached是danga.com的一个项目.它是一款开源的高性能的分布式内存对象缓存系统.最早是给LiveJournal提供服务的.后来逐渐被越来越多的大型站点所採用.用于在应用中减少对数据库 ...

  4. 【Docker】拉取Oracle 11g镜像配置

    以下是基于阿里云服务器Centos 7操作 1.拉取Oracle11g镜像 docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_1 ...

  5. LEGO机器人发展史

    1998,cybermaster 1999,RCX 1999,micro scout 2000,scout 2002,spybotics NXT NXT2 EV3

  6. 学习KNN

    转:© 著作权归作者所有 by ido 什么是KNN算法呢?顾名思义,就是K-Nearest neighbors Algorithms的简称.我们可能都知道最近邻算法,它就是KNN算法在k=1时的特例 ...

  7. git编译

    Git 是一个自由.开源.高效的分布式版本控制系统(VCS),它是基于速度.高性能以及数据一致性而设计的,以支持从小规模到大体量的软件开发项目.Git 是一个可以让你追踪软件改动.版本回滚以及创建另外 ...

  8. 在阿里云上进行Docker集群的自动弹性伸缩

    摘要: 在刚刚结束的云栖大会上,阿里云容器服务演示了容器的自动弹性伸缩,能够从容应对互联网应用的峰值流量.阿里云容器服务不仅支持容器级别的自动弹性伸缩,也支持集群节点级别的自动弹性伸缩.从而真正做到从 ...

  9. Linux-Linux下安装redis报错"undefined reference to__sync_add_and_fetch_4"解决办法

    如果出现这种错误可以在make的时候加上CFLAGS="-march=i686" 即 make CFLAGS="-march=i686" ----------- ...

  10. HDUOJ----4509湫湫系列故事——减肥记II

    湫湫系列故事——减肥记II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...