问题:

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. 注册OCX失败

    今天注册某个OCX时,Windows报告以下错误: 模块“XXX.ocx”已加载,但对 DllRegisterServer 的调用失败,错误代码为 0x80040200. 这是Windows权限引起的 ...

  2. 解决微信公众号OAuth出现40029(invalid code,不合法的oauth_code)的错误

    关于OAuth 官方教程:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842&token=&la ...

  3. C语言 · 冒泡排序

    for(int k=0;k<N;k++) {  for(int j=k+1;j<N;j++){    if(a[k]>a[j]){      int t = a[k];      a ...

  4. C# Azure 存储-分布式缓存Redis工具类 RedisHelper

    using System; using System.Collections.Generic; using Newtonsoft.Json; using StackExchange.Redis; na ...

  5. promise实现原理

    先看的这篇有问题的文章 花了很长时间研究这篇文章,卡在实现串行Promise那儿了,一直看不明白.就在刚才,发现这篇文章是错的,在第一次用setTimeout( ,0)那儿就错了.虽然用setTime ...

  6. JS.中文乱码,Jsp\Servlet端的解决办法

    JS.中文乱码,Jsp\Servlet端的解决办法 2010-03-08 15:18:21|  分类: Extjs |  标签:encodeuricomponent  乱码  urldecoder   ...

  7. Atitit webservice的发现机制 discover机制

    Atitit webservice的发现机制 discover机制 1.1. Ws disconvert 的组播地址和端口就是37021 1.2. Ws disconvert的发现机制建立在udp组播 ...

  8. Oracle 把秒转成时分秒格式(hh24:mm:ss);检测字符串是否是数字;字符串转换为数字

    不说废话,贴代码: CREATE OR REPLACE FUNCTION to_time(sec IN NUMBER) RETURN VARCHAR2 IS /*把秒转成时分秒格式 auth lzpo ...

  9. iOS-iOS开发简单介绍

    概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的 ...

  10. CSS系列:CSS的继承与层叠特性

    1. CSS的继承特性 所有的CSS语句都是基于各个标记直接的继承关系,CSS继承是指子标记会继承父标记的所有样式风格,并可以再父标记样式风格的基础上再加以修改,产生新的样式,而子标记的样式完全不会影 ...