Given a linked list, swap every two adjacent nodes and return its head.

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.

Note:

  • Your algorithm should use only constant extra space.
  • You may not modify the values in the list's nodes, only nodes itself may be changed.

要求把相邻的2个节点两两互换,还必须是换指针而不能是只换值

这里我们用递归的方法来处理,两个指针l1和l2,l1-->l2,我们先把l1和l2换了,然后对l1.next.next继续相同的方法递归下去

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if (head==null || head.next==null) return head;
ListNode res = head.next;
head.next = swapPairs(head.next.next);
res.next = head;
return res;
}
}

[LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点的更多相关文章

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

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

  2. LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)

    题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...

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

  4. 24. Swap Nodes in Pairs[M]两两交换链表中的节点

    题目 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the va ...

  5. (链表 递归) 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 ...

  6. LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)

    题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...

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

  8. Leetcode 24——Swap Nodes in Pairs

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

  9. LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

随机推荐

  1. myeclipse2014 安装maven3.3.9和maven配置本地仓库 及错误修改

    结合网上的知识梳理以及自己安装的经验 myeclipse2014 安装maven3.3.9和maven配置本地仓库  及犯的错误修改  成功搞定maven 1,安装 Maven 之前要求先确定你的 J ...

  2. [agc014d] Black and White Tree

    Description ​ 有一颗n个点的树,刚开始每个点都没有颜色. ​ Alice和Bob会轮流对这棵树的一个点涂色,Alice涂白,Bob涂黑,Alice先手. ​ 若最后存在一个白点,使得这个 ...

  3. quartz实例以及主要事项(注解)

    实现任务类: package com.vnetoo.nec.base.quartz; import org.springframework.context.annotation.Lazy;import ...

  4. Phpstudy+DiscuzX安装详解

    1.下载Discuz,地址:https://gitee.com/ComsenzDiscuz/DiscuzX/repository/archive/master.zip 2.下载phpstudy 3.将 ...

  5. IPython&Jupyter私房手册

    Jupyter是以Ipython为基础,可以极大的方便开发,对于如何使用,网上的资料都不太全.因此决定自己编写一个私房手册方便随时查找. 1. 安装和配置 安装不多说,不想折腾直接安装anaconda ...

  6. Zookeeper客户端对比选择_4

    Zookeeper客户端对比选择 本文思维导图 使用框架的好处是自带一套实用的API,但是Zookeeper虽然非常强大,但是社区却安静的可怕,版本更新较慢,下面会先从zookeeper原生API的不 ...

  7. PowerShell 如何 远程连接【转】

    转自: 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://bobzy.blog.51cto.com/2109336/1181249 ...

  8. qt线程睡眠

    头文件 #include <QThread> 接口函数: void QThread::sleep ( unsigned long secs )   [static protected] v ...

  9. ubuntu18.04 安装mongodb 数据库

    工具: 系统:ubuntu18.04  64位 数据库:mongodb GUI:Robo 3T           描述:在win 下面使用Robo 3T  连接Mongodb 数据库 一. 安装mo ...

  10. 洛谷 P4859 && BZOJ3622: 已经没有什么好害怕的了

    题目描述 给出 \(n\) 个数 \(a_i\)​ ,以及 \(n\) 个数 \(b_i\)​ ,要求两两配对使得 \(a>b\) 的对数减去 \(a<b\) 的对数等于 \(k\) . ...