[Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given1->2->3->4, you should return the list as2->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.
题意:成对交换结点。
思路:这题感觉是reverse nodes in k grops的是一个特殊情况。这题更简单一些,只要保证当前结点cur和其后继存在就可以交换。改变表头,所以要new一个。代码如下:
/**
* 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 *nList=new ListNode(-);
nList->next=head;
ListNode *pre=nList;
ListNode *cur=head; while(cur&&cur->next)
{
ListNode *temp=cur->next;
cur->next=temp->next;
temp->next=pre->next;
pre->next=temp;
pre=cur;
cur=cur->next;
} return nList->next;
}
};
[Leetcode] Swap nodes in pairs 成对交换结点的更多相关文章
- [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-&g ...
- [LeetCode]Swap Nodes in Pairs题解
Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- 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 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...
- 【LeetCode】Swap Nodes in Pairs(两两交换链表中的节点)
这是LeetCode里的第24题. 题目要求: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定1->2->3->4, 你应该返回2->1->4- ...
随机推荐
- GET POST 请求的详细区别
前言: 作为最常见的请求方式 在面试很有可能会被问到 所以在这里做一个简单总结 GET get方法向页面请求发送参数 地址和参数之间用?进行分割 例如 localhost:80/download.ht ...
- 第四课:PHP 变量
变量指程序中使用的数值是可以变化的量,与常量(一旦被定义,就无法改变)相反. 变量是用于存储信息的"容器": 实例 <?php $x=5; $y=6; $z=$x+$y; e ...
- Scala语法(三)
模式匹配 1)match val a = 1 val b=a match { *// a match { }返回值赋予变量 b case 1 => "red" case 2 ...
- 最简单的bootloader的编写
目标:写出bootloader的第一阶段代码和第二阶段代码,并测试. 最简单的bootloader的编写步骤: 1. 初始化硬件:关看门狗.设置时钟.设置SDRAM.初始化NAND FLASH2. 如 ...
- 用ssh进行git clone出现 fatal: Could not read from remote repository.
问题:在通过MobaXterm进行ssh连接的服务器上用ssh进行git clone出现 fatal: Could not read from remote repository. 解决方法:prox ...
- 初步学习pg_control文件之二
接前文:初步认识pg_control文件 继续学习,pg_control文件在何处形成的?是在initdb的时候,运用的函数如下: /* * This func must be called ONCE ...
- Phoenix映射HBase数据表
1. 说明 安装好phoenix后对于HBase中已经存在的数据表不会自动进行映射,所以想要再phoenix中操作HBase已有数据表就需要手动进行配置. 2. 创建HBase表 > creat ...
- 3.Linux 文件的压缩与打包
1.常用压缩打包命令 常用的压缩打包扩展名为如下: *.Z compress 程序压缩的文件,非常老旧了,不再细说 *.gz gzip 程序压缩的文件: *.bz2 bzip2 程序压缩的文件: *. ...
- ES5新增数组方法(1):filter
检测数组元素,并返回符合条件所有元素的数组. 1.过滤数组中不符合条件的元素 let arr = [1, 2, 3, 4, 5, 6]; // 方式一 let newArr = arr.filter( ...
- npm命令 VS yarn命令
npm yarn 说明 npm init yarn init 在项目中引导创建一个package.json文件 npm install yarn install/yarn 安装所有依赖包(依据pa ...