24.Swap Nodes in Pairs (List; Two-Pointers)
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* p1 = head;
ListNode* p2 = head->next;
p1->next = p2->next;
head = p2;
head->next = p1; ListNode* current = p1;
p1 = current->next;
while(p1 && p1->next){
p2 = p1->next;
p1->next = p2->next;
current->next = p2;
p2->next = p1; current = p1;
p1 = current->next;
} return head;
}
};
24.Swap Nodes in Pairs (List; Two-Pointers)的更多相关文章
- 24. Swap Nodes in Pairs
		
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
 - [Leetcode][Python]24: Swap Nodes in Pairs
		
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
 - 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)
		
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
 - leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
		
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
 - 【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] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
		
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
 - [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 成对交换节点
		
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
 - Java [leetcode 24]Swap Nodes in Pairs
		
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
 - 【LeetCode】24. Swap Nodes in Pairs
		
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
 
随机推荐
- DIY远程控制开关(tiny6410+LED+yeelink+curl)
			
上一次,介绍了如何实现远程监控室内温度,大家伙反响还是很热烈的,笔者很欣慰,独乐乐不如众乐乐啊.不过话说回来,那个实现只能是远程监测家中温度,假如发现家里热得很,想远程打开空调开关提前降降温,回家后不 ...
 - memcache+tomcat7.0.37+nginx实现session共享
			
一.session工作原理 由于http是无状态的协议,当我们访问了页面A,然后访问页面B,http无法确定这2个页面的访问是来自同一个人.因此,我们要用cookie或session来跟踪用户,根据授 ...
 - ajax向后台请求数据,后台接收到数据并进行了处理,但前台就是调用error方法
			
如果你的前台页面书写正确的情况下,并且运行情况和本文题目类似,那不妨试试这个: 在ajax方法中加上:async:false,让ajax同步执行. 因为ajax默认是异步的,至于为什么会不执行succ ...
 - 使用 Windows 10 中的加速度计(Accelerometer,重力传感器)
			
在做 UWP 应用开发的时候还有什么理由可以用到加速度计呢?场景很多啦,比如做游戏,做类似 Surface Hub 那种一边旋转,一边所有内容跟着一起转的效果. Windows 10 UWP 中的加速 ...
 - POJ2728 Desert King 最优比率生成树
			
题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...
 - IT项目管理的十六个字心得体会
			
目标驱动,系统思维,风险意识,数据量化 凡事预则立,不预则废.如果你不知道要到哪里?给你一张地图也没有用.目标驱动首先要有最基本的计划管理和时间管理能力.对于一个项目,我们过程中做的所有工作都是为了要 ...
 - intellij idea 清除版本控制
			
一.概述 intellij idea 再加入版本控制后,在工作空间中的项目文件都会纳入管理范围,这样idea左侧 "project视图" 中的项目及文件也会出现红色(可能其它颜色) ...
 - StringUtils.isEmpty()和isBlank()的区别
			
一.概述 两种判断字符串是否为空的用法都是在程序开发时常用的,相信不少同学在这种简单的问题上也吃过亏,到底有什么区别,使用有什么讲究,带着问题往下看. 二.jar包 commons-lang3-3.5 ...
 - Chrome 解决flash问题
			
Chrome 无法显示使用插件的内容 Chrome 不再支持很多插件.不过网站创建者已经通过更安全的方式,将多数这类功能添加到 Chrome 中. 为什么 NPAPI 插件现在无法正常运行过去,许多插 ...
 - 代码规范 for node.js with 'npm-coding-style'
			
npm-coding-style npm's "funny" coding style Description npm's coding style is a bit unconv ...