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. Eclipse 如何导入MyEclipse的项目

      Eclipse 如何导入MyEclipse的项目 CreateTime--2018年3月8日09:53:55 Author:Marydon 1.Eclipse导入MyEclipse的项目方法,跟导 ...

  2. 1个比较简单的使用java反射机制获取前台数据进行数据封装的例子

    //利用反射机制 和 从request获取所有参数的形式来构建查询对象 CustomerChooseSearchParma searchObject = new CustomerChooseSearc ...

  3. MYSQL在Win下免安装zip

    mysql对于Win测试简单zip安装即可了解my.in配置文件的妙处(show variables),本地搭建mysql服务测试引擎! 01.下载 https://dev.mysql.com/dow ...

  4. springboot + mybatis +easyUI整合案例

    概述 springboot推荐使用的是JPA,但是因为JPA比较复杂,如果业务场景复杂,例如企业应用中的统计等需求,使用JPA不如mybatis理想,原始sql调优会比较简单方便,所以我们的项目中还是 ...

  5. Google 做过的 12 件奇葩事

    Google做了太多伟大的事情了.以至于有时它有点让人难以实时跟上它的动态.假设你对这家公司略微有点感情.看看他们做过的一些有点匪夷所思的事儿,可能认为,毕竟是大公司.还挺难以被全然理解透的. 一个Q ...

  6. js 取值 getElementsByTagName,getElementsByName

    getElementsByTagName,getElementsByName  获取的值是数组的所以用[0][1]引用 1 <select multiple size="2" ...

  7. Kafka中Topic级别配置

    一.Kafka中topic级别配置 1.Topic级别配置 配置topic级别参数时,相同(参数)属性topic级别会覆盖全局的,否则默认为全局配置属性值. 创建topic参数可以设置一个或多个--c ...

  8. Ubuntu Pycharm不能同时选中多行解决方法

    转自http://blog.csdn.net/yaoqi_isee/article/details/77866309 问题描述 Pycharm和Sublime有一个很好用的特性就是可以同时选中多行进行 ...

  9. Python学习笔记010——形参与实参

    在使用中忽略了一个问题,形参有些和实参类似,也不能是“关键字后面含有位置参数”,即“默认形参”后面必须不能含有“位置”形参! def test(a=100,b): print("test&q ...

  10. Android: ListView数据的分批加载 以及 Handle 总结

    这是效果图: activity_main.xml 01 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ ...