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.

这道题我尝试写了用两个指针交换的方法,结果Runtime error了。

最后看了Discuss的解答,整理一下思路。首先,用指针的指针pp指向头部,a和b分别指向当前的node节点的指针指向下一个节点的指针。而我们要做到的是把 *pp == a -> b -> (b->next) 转换成 *pp == b -> a -> (b->next)。事实上代码中while循环内前三行做的就是这个任务。

代码如下:

class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode **pp = &head, *a, *b;
while ((a = *pp) && (b = a->next)) {
a->next = b->next;
b->next = a;
*pp = b;
pp = &(a->next);
}
return head;
}
};

[LeetCode]Swap Nodes in Pairs题解的更多相关文章

  1. LeetCode: Swap Nodes in Pairs 解题报告

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

  2. [LeetCode] 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

    1.题目描述 Given a linked list, swap every two adjacent nodes and return its head.   For example, Given ...

  4. Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置

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

  5. [LeetCode]Swap Nodes in Pairs 成对交换

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

  6. [Leetcode] Swap nodes in pairs 成对交换结点

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

  7. LeetCode Swap Nodes in Pairs 交换结点对(单链表)

    题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...

  8. leetcode Swap Nodes in Pairs python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

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

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

随机推荐

  1. 网卡NAT方式下虚拟机安装FTP服务

    在windows8下安装Oracle VM VirtualBox虚拟机,虚拟机中安装的CentOS操作系统,在CentOS中搭建LNMP环境,安装vsftpd服务器,宿主机在phpStorm编程,将代 ...

  2. JDK8的安装及环境配置

    原文链接:https://www.cnblogs.com/chenxj/p/10137221.html 1.下载JDK: a.直接官网下载:http://www.oracle.com/: b.或百度网 ...

  3. Keil软件编程问题归总

    Q:KEIL.uVision.RealView.MDK.KEIL C51之间的关系纠葛(比较区别)? A: KEIL是公司的名称,有时候也指KEIL公司的所有软件开发工具,目前2005年Keil由AR ...

  4. ui7

    2016.9讲义 一.课程的主要内容和目的 二.课程所用工具软件——Photoshop CS6 1. Photoshop 的发展史 1990.2,ps1.0问世,1991.2,PS2.0发行,此后,进 ...

  5. Mac下用Launchd实现使用rc.local执行开机启动命令

    其实原理很简单,使用Launchd创建一个开机启动的服务,然后这个服务关联/etc/rc.local文件,那么接下来操作rc.local就和Linux下一样的了. 当然,这种思路还可以直接使用在~/. ...

  6. (转)Python 字符串格式化 str.format 简介

    原文:https://www.cnblogs.com/wilber2013/p/4641616.html http://blog.konghy.cn/2016/11/25/python-str-for ...

  7. hibernate核心开发接口_Configuration

    AnnotationConfiguration继承自Configuration,这里以AnnotationConfiguration为例: new AnnotationConfiguration(). ...

  8. XXX is not in the sudoers file. This incident will be reported 的问题解决方案

    不多说,直接上干货! 说的是,这种问题,是出现在ubuntu系统里. root@SparkSingleNode:/usr/local/jdk# pwd /usr/local/jdk root@Spar ...

  9. Types方法之isCastable-isConvertible

    5. Conversions and Promotions 5.1. Kinds of Conversion 5.1.1. Identity Conversion 5.1.2. Widening Pr ...

  10. eclipse自定义代码注释

    效果如下: 自定义注释的方法: