[LeetCode]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题解的更多相关文章
- LeetCode: Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- leetcode—Swap Nodes in Pairs
1.题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [LeetCode]Swap Nodes in Pairs 成对交换
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...
- LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...
- leetcode Swap Nodes in Pairs python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
随机推荐
- [JS] 屏蔽右键
if (window.Event) document.captureEvents(Event.MOUSEUP); function nocontextmenu() { event.cancelBubb ...
- nginx的几种负载均衡策略
转自https://www.cnblogs.com/1214804270hacker/p/9325150.html 一.关于Nginx的负载均衡 在服务器集群中,Nginx起到一个代理服务器的角色(即 ...
- 支持向量机通俗导论(SVM学习)
1.了解SVM 支持向量机,因其英文名为support vector machine,故一般简称SVM,通俗来讲,它是一种二类分类模型,其基本模型定义为特征空间上的间隔最大的线性分类器,其学习策略便是 ...
- mybaits中"#"和"$"的区别
动态 sql 是 mybatis 的主要特性之一,在 mapper 中定义的参数传到 xml 中之后,在查询之前 mybatis 会对其进行动态解析.mybatis 为我们提供了两种支持动态 sql ...
- windows任务管理器所查的网站
添加menu https://blog.csdn.net/u012273127/article/details/71293088 点击菜单打开对话框 https://blog.csdn.net/ ...
- Hibernate 连接数据库,数据库返回数据超过限制报错
1.packet for query is too large 1024 >. you can change this value on the server mysql max_allowed ...
- Vue.js 动态挂载子组件
直接来实例 父组件 <template> <div> <div :is="sub.content" :ref="sub.name" ...
- 终于解决了贴吧手机版的一个重大BUG
终于解决了贴吧手机版的一个重大BUG 别诧异虽然同一个域名,但是,PC 和手机打开完全不一样的体验 http://tieba.yunxunmi.com/ 吃点夜校准备做梦去!! 发现 我云贴吧 一个 ...
- javascript中prototype与__proto__
1.prototype:构造函数独有的属性: __proto__:每个对象都有一个名为__proto__的属性: 注意:每个构造函数(自带与自创)都有一个prototype的属性,构造函数的proto ...
- 【Maven学习】远程仓库的配置
很多情况下,默认的中央仓库无法满足项目的需求,我们可能需要配置新的远程仓库,此时我们可以这样配置: <repository> <id>java-net</id> & ...