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 ...
随机推荐
- 那些实用的Nginx规则
1. 概述 大家都知道Nginx有很多功能模块,比如反向代理.缓存等,这篇文章总结下我们这些年实际环境中那些有用的Nginx规则和模块,大部分是用法的概括及介绍,具体细节在实际配置时再自行google ...
- 一台 linux 主机装两个mysql
启动 3306 nohup /usr/local/mysql5.1.7/bin/mysqld_safe & 启动 3307/usr/local/mysql/bin/mysqld --defau ...
- Springboot使用Cookie,生成cookie,获取cookie信息(注解与非注解方式)
先 创建一个控制类吧, 其实我没有分层啊,随便做个例子: MyGetCookieController: @RestControllerpublic class MyGetCookieControlle ...
- test20181016 B君的第三题
题意 B 君的第三题(haskell) 题目描述 大学四年,我为什么,为什么不好好读书,没找到和你一样的工作. B 君某天看到了这样一个题,勾起了无穷的回忆. 输入\(n, k\) 和一棵\(n\) ...
- NTP时间服务器的配置
1.NTP简介NTP(Network Time Protocol,网络时间协议)是用来使网络中的计算机,时间同步的一种协议.NTP服务器利用NTP协议来提供时间同步服务. 2 .环境准备主机名 ...
- CentOS 6.0 VNC远程桌面配置方法(转帖)
问题:新装开发机,安装VNC软件后,按照下面文档配置后,无法用VNC view连接,关闭防火墙后可以连上 解决方法:说明问题出在防火墙配置上,除了允许15900端口外,还有其他要设,经过排查后,加上如 ...
- Spring AOP 实现读写分离
原文地址:Spring AOP 实现读写分离 博客地址:http://www.extlight.com 一.前言 上一篇<MySQL 实现主从复制> 文章中介绍了 MySQL 主从复制的搭 ...
- angularJS的ng-repeat-start
使用angularJS的同学对ng-repeat都不会陌生,他是用来进行数据循环的,一般用于数组或者对象.但是今天我们用到了一个ng-repeat-start. ng-repeat-start,与ng ...
- linux中bin与sbin目录的作用及区别介绍
linux中bin与sbin目录的作用及区别介绍 本文介绍下,linux中的二个主要目录:bin与sbin,它们的作用与区别,学习linux的朋友可以参考下 在linux系统中,有两个重要的目录:bi ...
- CentOS之NTP服务器配置
本文使用CentOS 6.5作为搭建环境 一.服务器端配置 1.安装所需软件包 yum -y install ntp ntpdate---------------------------------- ...