【Swap Nodes in Pairs】cpp
题目:
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.
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
if (!head || !(head->next) ) return head;
ListNode dummy(-);
dummy.next = head;
ListNode *prev = &dummy;
ListNode *curr = head;
while ( curr && curr->next )
{
prev->next = curr->next;
curr->next = curr->next->next;
prev->next->next = curr;
prev = curr;
curr = curr->next;
}
return dummy.next;
}
};
Tips:
链表基本操作,动手画图,直接出来。
===================================
第二次过这道题,一次AC了。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode dummpy(-);
dummpy.next = head;
ListNode* pre = &dummpy;
ListNode* curr = head;
while ( curr && curr->next )
{
pre->next = curr->next;
curr->next = curr->next->next;
pre->next->next = curr;
pre = curr;
curr = curr->next;
}
return dummpy.next;
}
};
【Swap Nodes in Pairs】cpp的更多相关文章
- leetcode 【 Swap Nodes in Pairs 】python 实现
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- 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 ...
- 【Reverse Nodes in k-Group】cpp
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- 【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】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- Leetcode-24 Swap Nodes in Pairs
#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
随机推荐
- 360 so动态脱壳
环境及工具 手机 : 中兴 U887 系统版本: Android 2.3.5 工具 : IDA pro 6.6 .0101Editor 版权声明:未经许可,随便转载 目前so加壳有 ...
- 【c++】用c++编写的求任意区间的素数的小程序
#include using namespace std; int main() { cout<<"*************************************** ...
- 关于dom4j在maven中的使用
在maven中添加dom4j的依赖, (如何使用eclipse构建maven项目, 这里就不在赘述) <!-- https://mvnrepository.com/artifact/org.do ...
- 559. N 叉树的最大深度
给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度,3. 说明: 树的深度不会超过 1000.树的节点总 ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- yii2 详细分解实现分页效果
1.首先,准备一个数据表,内容为: 然后建立一个控制器,比如我的例子中的是:PractiseController.php 接着,我们开始在控制器中输入内容: 从中我们可以看到,这是将表数据中,id为2 ...
- IOS NSKeyedArchiver(归档存取数据)
如果对象是NSString.NSDictionary.NSArray.NSData.NSNumber等类 型,可以直接用NSKeyedArchiver进行归档和恢复 不是所有的对象都可以直接用这种方法 ...
- 扫描局域网ip的shell
# vim /mysh/ipscan.sh #!/bin/bash # scan the local alive ipaddress # -- if [ -f $filename ];then ech ...
- 2018.6.7. 云服务器Centos系统使用yum或者rpm安装包时出现问题,安装时报出错误:
当我向终端输入 sudo yum groupinstall chinese-support 语言安装包的时候显示下面的错误 error: rpmdb: BDB0113 Thread/process 3 ...
- python_65_生成器1
# map()函数 # map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. # 例 ...