问题:

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. ASP.NET MVC 5 - 将数据从控制器传递给视图

    在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图.控制器类将响应请求来的URL.控制器类是给您写代码来处理传入请求的地方,并从数据库中检索数据,并最终决定什么类型的返回结果 ...

  2. Gamma函数是如何被发现的?

    学过微积分的人,肯定都接触过Euler积分,按教科书上的说法,这是两种含有参变量的定积分,但其实没那么玄乎,它们只是两个函数.其中第一型Euler积分叫\(B\)-函数,第二型Euler积分叫\(\G ...

  3. 浅谈android中的目录结构

    之前在android游戏开发中就遇到本地数据存储的问题:一般情形之下就将动态数据写入SD中存储,在没有SD卡的手机上就需另作处理了;再有在开发android应用的过程中,总要去调试APP,安装时又想去 ...

  4. 程序中保存状态的方式之Cookies

    程序中保存状态的方式之 Cookies,之前写过一篇关于ViewState的.现在继续总结Cookies方式的 新建的测试页面login <%@ Page Language="C#&q ...

  5. C#需知--长度可变参数--Params

    Params用于参数的数量可变的情况下,即参数的个数是未知数. 使用Params需要知道以下几点: 1.如果函数传递的参数含有多个,使用Params标记的参数数组需要放在最后 图上显示的很明确,不需要 ...

  6. eclipse将android项目生成apk并且给apk签名

    转载:http://www.cnblogs.com/tianguook/archive/2012/09/27/2705724.html 生成apk最懒惰的方法是:只要你运行过android项目,到工作 ...

  7. Backup Volume 操作 - 每天5分钟玩转 OpenStack(59)

    本节我们讨论 volume 的 Backup 操作. Backup 是将 volume 备份到别的地方(备份设备),将来可以通过 restore 操作恢复. Backup VS Snapshot 初看 ...

  8. JS+CSS3实现带预览图幻灯片效果

    这个案例学习起来还有点吃力,目前还没有独自自己写出来过,贴出来以免忘记. 慕课网该课程原地址:http://www.imooc.com/learn/412 源码: <!DOCTYPE html& ...

  9. AngularJs 动态加载模块和依赖

    最近项目比较忙额,白天要上班,晚上回来还需要做Angular知识点的ppt给同事,毕竟年底要辞职了,项目的后续开发还是需要有人接手的,所以就占用了晚上学习的时间.本来一直不打算写这些第三方插件的学习笔 ...

  10. BAT及各大互联网公司2014前端笔试面试题--Html,Css篇

    很多面试题是我自己面试BAT亲身经历碰到的.整理分享出来希望更多的前端er共同进步吧,不仅适用于求职者,对于巩固复习前端基础更是大有裨益. 而更多的题目是我一路以来收集的,也有往年的,答案不确保一定正 ...