题目描述: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. 调度《Taint(污点) 和 Toleration(容忍)》

    节点亲和性(详见这里),是 pod 的一种属性(偏好或硬性要求),它使 pod 被吸引到一类特定的节点.Taint 则相反,它使 节点 能够 排斥 一类特定的 pod. Taint 和 tolerat ...

  2. 水题挑战1:NOIP 2013 选择客栈

    丽江河边有\(n\) 家很有特色的客栈,客栈按照其位置顺序从 \(1\) 到 \(n\) 编号.每家客栈都按照某一种色调进行装饰(总共 \(k\) 种,用整数 \(0 \sim k-1\) 表示),且 ...

  3. [BZOJ 2287/POJ openjudge1009/Luogu P4141] 消失之物

    题面: 传送门:http://poj.openjudge.cn/practice/1009/ Solution DP+DP 首先,我们可以很轻松地求出所有物品都要的情况下的选择方案数,一个简单的满背包 ...

  4. 【SpringBoot】01.创建Springboot项目及启动器

    创建Springboot项目及启动器 1.创建一个简单maven项目 SpringBoot2.0以下需要使用JDK1.7 ,2.0以上使用JDK1.8 如果需要修改JDK的版本需要打开pom文件: & ...

  5. Linux 软件安装的三种方式

    Linux 软件安装的三种方式 1.yum ​ 语法格式: ​ yum -y install package.name ​ -y yes # 遇到提示自动输入yes ​ 案例: 安装ifconfig命 ...

  6. [C#.NET 拾遗补漏]12:死锁和活锁的发生及避免

    多线程编程时,如果涉及同时读写共享数据,就要格外小心.如果共享数据是独占资源,则要对共享数据的读写进行排它访问,最简单的方式就是加锁.锁也不能随便用,否则可能会造成死锁和活锁.本文将通过示例详细讲解死 ...

  7. ctf堆叠注入总结(持续更新)

    第一种 1';USE db_name;SET @sql=CONCAT('sql contents');PREPARE stmt_name FROM @sql;EXECUTE stmt_name; 第二 ...

  8. waf 引擎云原生调研---扫盲

    概念: lstio Istio是一个用于服务治理的开放平台 Istio是一个Service Mesh形态的用于服务治理的开放平台 Istio是一个与Kubernetes紧密结合的适用于云原生场景的Se ...

  9. #paragma详解

       #Pragma是预处理指令,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#Pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统 ...

  10. 异常记录-Gradle依赖掉坑之旅

    前言 最近在项目中遇到了一个问题,死活拉不下来依赖,耗费了一整天,感觉自己真是菜的抠脚. 没想到今天脑子一清醒,刷刷的问题逐个击破了. 问题描述: 项目成员添加了新的依赖,然后我这边项目拉下来,bui ...