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. C语言-字、半字、内存位宽相关

    1.32位系统:32位系统指的是32位数据线,但是一般地址线也是32位,这个地址线32位决定了内存地址只能有32位二进制,所以逻辑上的大小为2的32次方.内存限制就为4G.实际上32位系统中可用的内存 ...

  2. elasticsearch-logstash

    1.logstash介绍 logstash 是ES 下的一款开源软件.用于数据采集,就是从Mysql等数据源采集数据.更新数据.然后将数据发送到ES中创建.更新索引 2.安装 演示环境是windows ...

  3. jQuery下锚点的平滑跳转

    对于锚点的平滑跳转,我觉得要谨慎使用,在个人站点或是这个效果含有功能提示可以用一用,在一般的商业性质的网站上,权衡来讲,不用更好,当然,这只是我的个人意见.jQuery库已经为我们做了很多的工作了,所 ...

  4. InnoDB和MyISAM区别总结

    原来是MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持. MyISAM类型的表强调的是性能,其执行数度比InnoDB类型更快,但是不提供事务支持,而InnoDB提供事务支持已经外部键等 ...

  5. 用Python3生成30万条excel数据(xlsx格式)

    在B/S架构的系统测试中,有时需要通过导入excel文件来生成一些数据记录,当数据量小的时候,一般不会出现什么问题,而当导入的数据量巨大时,对系统的性能就是一个考验了.为了验证系统的性能,有时需要导入 ...

  6. 蓝桥杯剪格子dfs

    #include<iostream> #include<cstring> #include<iomanip> #include<cmath> #incl ...

  7. CodeForces 1005D Polycarp and Div 3(思维、贪心、dp)

    http://codeforces.com/problemset/problem/1005/D  题意: 给一个仅包含数字的字符串,将字符串分割成多个片段(无前导0),求这些片段里最多有多少是3的倍数 ...

  8. android studio 导入eclipse项目后的报错解决

    1.如何导入: 2.导入的时候会让你创建gradle,一直下一步,不用修改 3.编译, a.会报编码格式错误,如果有中文会报这个错,修改成utf-8 b.找不到类,解决办法 右键工程 引入外部的包,重 ...

  9. codeforce 1189C Candies! ----前缀和

    题目大意:给你一个数组每个数不大于9,然后给你m个区间,每个区间的长度都是2的k次方(k=0 1 2.....)  有一种操作是把奇数位和偶数位相加  用和来代替之前的两个数,如果和大于等于10就要膜 ...

  10. ubuntu19.10——snap错误has install-snap change in progress

    使用软件商店安装时遇到问题 snap has install-snap change in progress 原因是之前的安装错误终止,使得现在的安装无法进行,解决方案: 终端输入: snap cha ...