问题:

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. 精简版StringBuilder,提速字符串拼接

    编写目的 在频繁的字符串拼接中,为了提升程序的性能,我们往往会用StringBuilder代替String+=String这样的操作; 而我在实际编码中发现,大部分情况下我用到的只是StringBui ...

  2. AngularJS2 + ASP.NET MVC项目

    环境:VS2015, NodeJS:v 6.5, npm: v3.10, AngularJs 2 通过将ASP.NET MVC项目与Angualr 2官网上的quick start整合的过程中遇到些问 ...

  3. Atitit 设计模式的本质思考】

    Atitit 设计模式的本质思考] 1. 世界就是有模式构建的1 1.1. 多次模式与偶然模式1 1.2. 模式就是在一种场合下对某个问题的一个解决方案."1 1.3. 模式需要三样东西.  ...

  4. 解决升级Win 10 IP 10122后无法调试UAP应用的方法

    可能各位也像老周一样,第一时间升级了Build 10122,但不知道大家有没有发现,当你升级后,写一个UAP应用,要调试运行时,就会出错,错误如下: DEP0730 : 注册应用程序失败,因为目标计算 ...

  5. Unity基础知识学习笔记一

      1,Unity 4.5.4这个版本,在打包ios应用程序的时候.会生成一个xcode工程,但是这个工程无法在xcode6上变异,无法生成ios8上发布.所以unity在ios8发布3天内就发了4. ...

  6. Android 如何制作九宫格图片(.9.png)

    对于编程人员来说,尤其是前端设计设计师,九宫格图片是必须的(.9.png),对于初学者来说不知道这个九宫格图片有什么用,其实这个九宫格图片实际常用在Android的button组件.要上下拉升的背景图 ...

  7. MySQL utf8mb4 字符集:支持 emoji 表情符号

    转载地址:http://www.linuxidc.com/Linux/2013-05/84360.htm 我用他的方法解决了问题,亲测可用,不要用Nnvicat for Mysql去查询编码,在服务器 ...

  8. iOS开发之微信聊天页面实现

    在上篇博客(iOS开发之微信聊天工具栏的封装)中对微信聊天页面下方的工具栏进行了封装,本篇博客中就使用之前封装的工具栏来进行聊天页面的编写.在聊天页面中主要用到了TableView的知识,还有如何在俩 ...

  9. input(file)按钮美化

    <!DOCTYPE HTML> <html> <body> <input type="file" id="upload" ...

  10. php+phpStorm+xdebug配置方法

    1.下载xdebug文件 http://xdebug.org/wizard.php 将phpinfo()的源代码复制到文本框中,xdebug会提示如何配置和下载哪个版本的xdebug. 全部下载地址: ...