[刷题] 24 Swap Nodes in Paris
要求
- 给定一个链表,对于每两个相邻的节点,交换其位置
示例
- 1->2->3->4->NULL
- 2->1->4->3->NULL


实现
1 struct ListNode {
2 int val;
3 ListNode *next;
4 ListNode(int x) : val(x), next(NULL) {}
5 };
6
7 class Solution {
8 public:
9 ListNode* swapPairs(ListNode* head) {
10 ListNode* dummyHead = new ListNode(0);
11 dummyHead->next = head;
12
13 ListNode* p = dummyHead;
14 while( p->next && p->next->next ){
15 ListNode* node1 = p->next;
16 ListNode* node2 = node1->next;
17 ListNode* next = node2->next;
18
19 node2->next = node1;
20 node1->next = next;
21 p->next = node2;
22
23 p = node1;
24 }
25
26 ListNode* retNode = dummyHead->next;
27 delete dummyHead;
28
29 return retNode;
30 }
31 };
相关
- 25 Reverse Nodes in k-Group
- 147 Insertion Sort List
- 148 Sort List
[刷题] 24 Swap Nodes in Paris的更多相关文章
- 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 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- 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 ...
- 24. Swap Nodes in Pairs 链表每2个点翻转一次
[抄题]: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2 ...
- [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
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
随机推荐
- Alluxio+HDFS+MapReduce集成及测试
目录 1.在 HDFS 上配置 Alluxio 1.1.节点角色 1.2.软件版本 1.3.准备工作 1.3.1.设置 SSH 免密登录 1.3.2.安装 JDK 1.3.3.安装 Hadoop 1. ...
- Docker系列——InfluxDB+Grafana+Jmeter性能监控平台搭建(二)
在上一篇博文中,主要是讲了InfluxDB的配置,博文链接:https://www.cnblogs.com/hong-fithing/p/14453695.html,今天来分享下Jmeter的配置. ...
- 201871030119-马桂婷 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告
项目 内容 课程班级博客 2018卓越工程师班 这个作业要求链接 实验三 软件工程结对项目 我的课程学习目标 1.体验软件项目开发中的两人合作,练习结对编程:2.掌握Github协作开发程序的操作方法 ...
- 并发编程(ReentrantLock&&同步模式之顺序控制)
4.13 ReentrantLock 相对于 synchronized 它具备如下特点 可中断 可以设置超时时间 可以设置为公平锁 支持多个条件变量,即对与不满足条件的线程可以放到不同的集合中等待 与 ...
- day-9 xctf-int_overflow
xctf-int_overflow 题目传送门:https://adworld.xctf.org.cn/task/answer?type=pwn&number=2&grade=0&am ...
- Day06_30_抽象类(Abstract)
抽象类 Abstract 什么是抽象类? 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就 ...
- SpringCloud(六)Bus消息总线
Bus 消息总线 概述 分布式自动刷新配置功能 Spring Cloud Bus 配合 Spring Cloud Config使用可以实现配置的动态刷新 Bus支持两种消息代理:RabbitMQ和Ka ...
- 【CPU100%排查】CPU100%问题排查方案
1.使用top -c 查看CPU 占用情况 ,按P(大写)可以倒序查看占CPU占用率 2.找到占用率高的进程以后,再定位到具体线程 比如 此时进程ID 14724 CPU占用高,进一步使用top - ...
- 【OOM】记录一次生产上的OutOfMemory解决过程
一.项目架构 SpringCloud Dalston.SR1 + SpringBoot 1.5.9 + Mysql +Redis + RabbitMQ 所有的业务模块的应用服务都部署在同一个服务器, ...
- Android NDK编程之Android.mk和Application.mk
Android编程使用NDK必须创建一个jni文件夹,并且jni文件里一般包含有C/C++的源码文件.Android..mk文件.Application.mk文件(可选),Android.mk文件的编 ...