问题:

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.

官方难度:

Easy

翻译:

给定一个链表,交换它们每两个的相邻节点,并且返回头结点。

算法必须使用恒定的空间,且不能只交换节点的数据,必须交换节点。

例子:

给定链表:1->2->3->4。

返回链表:2->1->4->3。

  1. 题目规定只能交换节点,如果只需交换数据就简单了。
  2. 首先,定义返回的头结点first,对于长度大于1的链表,返回的头结点是原链表的第二个节点。
  3. 维护当前节点的上一个节点before和下一个节点after。
  4. 进行循环,终止条件是当前节点或下一个节点为null。
  5. 循环内部,首先对after节点赋值,然后依次赋值before,head,after节点的next指针。
  6. 循环结束前,为下一次的循环赋值,替换before和head节点。

解题代码(交换数据):

     // 交换结点存放的数据
public static ListNode swapPairsVal(ListNode head) {
ListNode first = head;
while (head != null && head.next != null) {
// 交换数据,一步到位,但有溢出的风险
head.val = head.val + head.next.val - (head.next.val = head.val);
head = head.next.next;
}
return first;
}

swapPairsVal

解题代码(交换节点):

     // 交换结点
public static ListNode swapPairs(ListNode head) {
// 第一个节点会变
ListNode first = head == null || head.next == null ? head : head.next;
// 上一个节点
ListNode before = new ListNode(0);
ListNode after = new ListNode(0);
while (head != null && head.next != null) {
// 下一个节点
after = head.next;
// 交换节点
before.next = after;
head.next = after.next;
after.next = head;
// 下一次交换赋值
before = head;
head = head.next;
}
return first;
}

swapPairs

相关链接:

https://leetcode.com/problems/swap-nodes-in-pairs/

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/easy/Q024.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

No.024:Swap Nodes in Pairs的更多相关文章

  1. leetcode:Swap Nodes in Pairs

    Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...

  2. LeetCode OJ:Swap Nodes in Pairs(成对交换节点)

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

  3. [LeetCode 题解]:Swap Nodes in Pairs

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a li ...

  4. LeetCode 024 Swap Nodes in Pairs

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

  5. leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现

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

  6. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  7. [LintCode] Swap Nodes in Pairs 成对交换节点

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

  8. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

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

  9. 【LeetCode练习题】Swap Nodes in Pairs

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

随机推荐

  1. 优化MySchool数据库设计之【巅峰对决】

    优化MySchool数据库设计 之独孤九剑 船舶停靠在港湾是很安全的,但这不是造船的目的 By:北大青鸟五道口原玉明老师 1.学习方法: 01.找一本好书 初始阶段不适合,可以放到第二个阶段,看到知识 ...

  2. easyuidatagrid中load,reload,loadData的区别

    摘要:datagrid中有load,reload,loadData那三个方式,皆是加载数据的,但又有差别.下面让我们一起来看看: 首先,load方法,比如我已经定义一个datagrid的id为grid ...

  3. JS原生第六篇 (帅哥)

    复习 按钮不可用    disabled =  "disabled"  ||  true setTimeout   只执行一次    setInterval  执行很多次 递归调用 ...

  4. OPEN CASCADE Curve Continuity

    OPEN CASCADE Curve Continuity eryar@163.com Abstract. 设计一条复杂曲线时,出于设计和制造上的考虑,常常通过多段曲线组合而成,这就需要解决曲线段之间 ...

  5. 【转】java中注解的使用与实例

    原文:http://www.cnblogs.com/pepcod/archive/2013/02/20/2918719.html java 注解,从名字上看是注释,解释.但功能却不仅仅是注释那么简单. ...

  6. linux自定义系统调用

    1 Linux3.10.21内核系统调用设置 以前看的内核版本时2.6.11的,里面的系统调用设置一目了然啊!在文件entry.S中直接定义了sys_call_table表,并在这个文件中用各个系统调 ...

  7. Android样式之selector

    日常开发当中,难免会出现这样一种情况,为一个按钮.TextView...设置一个点击状态的颜色改变,可能是background背景的改变,也可能是字体颜色的改变,简单点说:默认状态下,字体颜色或者背景 ...

  8. @Async in Spring--转

    原文地址:http://www.baeldung.com/spring-async 1. Overview In this article we’ll explore the asynchronous ...

  9. 如何在程序中给word文档加上标和下标

    如何在程序中给word文档加上标和下标 上标或下标是一个小于普通行格式的数字,图形,标志或者指示通常它的设置与行相比偏上或偏下.下标通常显示于或者低于基准线,而上标则高于.上标和下标通常被用于表达公式 ...

  10. java 线程池ThreadPoolExecutor 如何与 AsyncTask() 组合使用。

    转载请声明出处谢谢!http://www.cnblogs.com/linguanh/ 这里主要使用Executors中的4种静态创建线程池实例方法中的 newFixedThreadPool()来举例讲 ...