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. Net Manager测试连接测试没有成功,用户权限问题,以管理员身份运行后测试成功

    Net Manager测试连接测试没有成功,截图如下:

  2. memcache面试题汇总

    1,memcached是原子的吗? 所有的被发送到memcached的单个命令是完全原子的.如果您针对同一份数据同时发送了一个set命令和一个get命令,它们不会影响对方.它们将被串行化.先后执行.即 ...

  3. (四)SSO之CAS框架单点登录,自定义验证登录方式

    应需求的变化,在登录cas的时候,默认根据用户名和密码进行验证,如果加上用户名,密码和一个系统标识进行验证呢?该如何做呢? 我们知道cas默认的登录界面中,输入的用户名和密码,再配置一下deploye ...

  4. mxonline实战5,用户注册的验证码

        github对应地址:验证码好麻烦     一. 安装 配置 1. pip install django-simple-captcha 2. add captcha to the INSTAL ...

  5. 基本项目框架搭建 sqlserver druid配置

    1.  我的连接池采用的是阿里云的druid的连接池,工具是IDEA 框架是springboot+maven 以下是我的项目框架结构 2. pom  中配置 <?xml version=&quo ...

  6. time 模块学习

    前情提要: time模块是经常使用的模块.主要是用来记录时间,以及时间上的相关操作 一:时间戳 1:第一种形式 import time print(time.time()) 从1970 1 1 0:0 ...

  7. CF1007D. Ants(树链剖分+线段树+2-SAT及前缀优化建图)

    题目链接 https://codeforces.com/problemset/problem/1007/D 题解 其实这道题本身还是挺简单的,这里只是记录一下 2-SAT 的前缀优化建图的相关内容. ...

  8. WebGIS简单实现一个区域炫酷的3D立体地图效果

    1.别人的效果 作为一个GIS专业的,做一个高大上的GIS系统一直是我的梦想,虽然至今为止还没有做出一个理想中的系统,但是偶尔看看别人做的,学习下别人的技术还是很有必要的.眼睛是最容易误导我们的,有时 ...

  9. html网页如何传递接收地址参数?

    实现html页面的参数传递 方法一: 下面是javascrīpt的一种实现方法, 这个函数是通过window.location.href中的分割符获得各个参数. 有了这个函数,就可以在页面之间传递参数 ...

  10. hibernate的反向生成改懒加载的地方

    改变懒加载只需要把生成的文件中的获取类型改为eager fetch = FetchType.EAGER @ManyToOne(fetch = FetchType.EAGER)//把懒加载换成饿加载模式 ...