题目描述:Swap Nodes in Pairs

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 == nullptr || head->next == nullptr)
return head; ListNode dummy(-1);
dummy.next = head; for(ListNode *prev = &dummy, *cur = prev->next, *next = cur->next;
next;
prev = cur, cur = cur->next, next = cur ? cur->next: nullptr) {
prev->next = next;
cur->next = next->next;
next->next = cur;
}
return dummy.next;
}
};

LeetCode 024 Swap Nodes in Pairs的更多相关文章

  1. Java for LeetCode 024 Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  2. LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点

    Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ...

  3. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  4. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  5. 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 ...

  6. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  7. 【LeetCode】024. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  8. [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 ...

  9. 【leetcode】Swap Nodes in Pairs (middle)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

随机推荐

  1. Linux下开发环境的搭建(For C++ OIer)

    说句实话,对于OIer来说,Linux真的是个很好的开发平台. 这里既没有游戏的喧嚣,也没有广告的打扰,gcc/g++早已预装,一切已为你准备好......(???)即使对于日常使用,也绰绰有余. 如 ...

  2. 基于MVC的RESTFul风格API实战

    基于MVC的RESTful风格的实现 1.RESTful风格阐述 REST服务是一种ROA(Resource-Oriented Architecture,面向资源的架构)应用.主要特点是方法信息存在于 ...

  3. Linux 环境下 C++ 的开发编译

    Linux环境下C++程序的开发编译学习笔记 环境:vmware 运行下的Ubuntu 16.04 姓名:谢津 时间:2018/5/24 内容:1)vim的安装及配置:2)第一个C++程序的编写与编译 ...

  4. ubuntu 17.10 安装QQ

    折腾一大堆 看报错信息 正在选中未选择的软件包 wine-qqintl:i386.(正在读取数据库 ... 系统当前共安装有 185429 个文件和目录.)正准备解包 wine-qqintl_0.1. ...

  5. 8、Django之模型层第三篇:更多字段与参数

    1 ORM字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. IntegerField 一个整数类型 ...

  6. dict和list

    一.字典(Dictionary) 1.什么是 dict(字典) 上一章节,我们学习了列表(List) 和 元组(tuple) 来表示有序集合. 而我们在讲列表(list)的时候,我们用了列表(list ...

  7. WSL-Ubuntu18.04 磁盘迁移 与 ns3-gym 安装

    WSL 安装 win10 版本应大于或等于 1903 win10 设置页面 输入 控制面板 并点击进入 找到 程序和功能 并打开 找到 启动或关闭 Windows 功能 并打开 向下拉 勾选 适用于L ...

  8. 学习笔记——ESP8266项目的例子编译时发生cannot find -lstdc++问题的解决

    在尝试对进行ESP8266项目的例子进行编译时发生cannot find -lstdc++问题 第一想法是安装libstdc++,结果安装时又发生了下面的情况: 再次查找原因,最后发现当前安装的交叉编 ...

  9. 流编辑器:sed

    一 简介:sed是一个精简的.非交互式的流式编辑器,它在命令行中输入编辑命令和指定文件名,然后在屏幕上查看输出.逐行读取文件内容到临时缓冲区,称为模式空间.接着用sed命令处理缓冲区内容,处理完之后, ...

  10. 三:登录功能实现,servlet

    1)servlet 2)request 请求对象 3)response响应对象 4)转发 5)重定向 1.servlet就是用来处理客户端的请求的 1.1去官网下载 1.2 在STS上添加该包ctrl ...