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

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.
/**
* 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 nxt = head.next;
ListNode newHead = swapPairs(nxt.next);
nxt.next = head;
head.next = newHead;
return nxt;
}
}

[LC] 24. Swap Nodes in Pairs的更多相关文章

  1. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  2. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

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

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

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

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

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

  7. 24. Swap Nodes in Pairs 链表每2个点翻转一次

    [抄题]: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2 ...

  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. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

随机推荐

  1. Vue中Js动画 与Velocity.js 多组件多元素 列表过渡

    Vue提供我们很多js动画钩子 写在tansition标签内部 入场动画 @before-enter="" 处理函数收到一个参数(e l) el为这个元素 @enter=" ...

  2. 常用DOS命令(1) color,dir,copy,shutdown,mkdir(md),rmdir(rd),attrib,cd

    1.  color color [attr] 设置默认的控制台前景和背景颜色. attr        指定控制台输出的颜色属性.颜色属性由两个十六进制数字指定 -- 第一个对应于背景,第二个对应于前 ...

  3. 关系数据库和NOSQL比较

    关系数据库 NOSQL 功能:       NOSQL            功能简单           基本只支持主键查询,有的NOSQL支持非主键查询(不过非主键查询时,其性能也很慢),很少有N ...

  4. C++之namespace、bool

    namespace: 1.namespace:标识符的各种可见范围.C++ 标准程序库中的所有标识符都被定义在一个名为  std 的namespace中. 2.当使用<iostream>的 ...

  5. 处理Ajax请求跨域问题

    ajax跨域的原理 ajax出现请求跨域错误问题,主要原因就是因为浏览器的“同源策略”. CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resourc ...

  6. Python笔记_第三篇_面向对象_5.一个关于类的实例(人开枪射击子弹)

    1. 我们学了类的这些东西,用这些类我们来操作一个关于类的实例. 2. 题目:人开枪射击子弹,然后具有装弹动作,然后再开枪. 第一步:设计类: 人类名:Person属性:gun行为:fire,fill ...

  7. 怎么通过scanf读取一个空白前的字符

    /************************************************************************* > File Name: scanf2.c ...

  8. ubuntu linux下解决“no java virtual machine was found after searching the following locations:”的方法

    现象:删除旧的jdk,安装新的jdk之后,打开eclipse报错: A Java Runtime Environment (JRE) or Java Development Kit (JDK)must ...

  9. 菜鸟cmake使用

    cmake是用过把源码生成visual studio 工程的工具,也就是生成.sln文件.他会把相应的库依赖都自动添加上. cmake有个CMakeLists.txt (具体语法这里先不介绍) 我都是 ...

  10. Hibernate一级缓存Session和对象的状态

    建议看原文:https://blog.csdn.net/qq_42402854/article/details/81461496 一.session简介        首先,SessionFactor ...