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.

---------------------------------------------------------------------------
中文大意是两两交换其中相邻的结点。有非递归和递归的解法,在非递归的解法上,要画下图,辅助理解。 C++代码:
非递归:
/**
* 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) {
ListNode *dummy = new ListNode(-),*cur = dummy;
cur->next = head;
while(cur->next && cur->next->next){
ListNode *t = cur->next->next;
cur->next->next = t->next;
t->next = cur->next;
cur->next = t;
cur =cur->next->next;
}
return dummy->next;
}
};

递归:

/**
* 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 || !head -> next){
return head;
}
ListNode *t = head->next;
head->next = swapPairs(head->next->next);
t->next = head;
return t;
}
};

参考博客:http://www.cnblogs.com/grandyang/p/4441680.html

(链表 递归) 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 (双数交换节点) 解题思路和方法

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

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

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

  4. leetcode 24. Swap Nodes in Pairs(链表)

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

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

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

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

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

  7. [leetcode]24. Swap Nodes in Pairs交换链表的节点

    感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...

  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 24. Swap Nodes in Pairs(详细图解一看就会)

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

随机推荐

  1. 二、Docker部署应用

    一.有关Docker的安装请参考docker官网  Docker 提供了两个版本:社区版 (CE) 和企业版 (EE). Docker 社区版 (CE) 是开发人员和小型团队开始使用 Docker 并 ...

  2. vim的几个常用操作

    现在很少会有人用vim来写代码,所以vim更常用在server上面编辑配置文件或者少量代码编辑: vim操作命令非常之多,如果仅用作一个配置文件的编辑器,掌握几个常用的操作就够了: 常用的操作其实就是 ...

  3. JQ和JS获取元素

    <ul>   <li>John</li> <li>Karl</li> <li>Brandon</li> </u ...

  4. Centos虚拟环境工具virtualenvwrapper

    下载安装virtualenvwrapper pip3 install virtualenvwrapper !!!!注意安装virtualenvwrapper必须是在本地环境下!!! 设置Linux的用 ...

  5. Spring 使用介绍(六)—— AOP(二)

    一.切入点语法 1)通配符 AOP支持的通配符: *:匹配任何数量字符 ..:匹配任何数量字符的重复,在类型模式中匹配任何数量子包,在方法参数模式中匹配任何数量参数 +:匹配指定类型的子类型,仅能作为 ...

  6. 安卓Android基础—第一天

    1.1G-4G的介绍 1G 大哥大 2G 小灵通 采用gsm标准(美国军方标准民用化) 发短信 3G 沃 72M/s 4G lte 100M/s 5G 华为 10G/s 小公司卖茶品大公司卖版权(标准 ...

  7. 洛谷P4281 紧急集合 / 聚会

    LCA 题目要求找离三个点最近的点,我们先看两个点的情况,自然是找LCA,那么三个点的时候是否与LCA有关呢? 显然,离三个点最近的点一定是在这三个点联通的简单路径上. 可以简单证明一下,假设某个点离 ...

  8. python的小练习

    # -*- coding: utf-8 -*- """练习:有1,2,3,4. 4个数能组成多少个互不相同且无重复数字的三位数,分别是多少?""&qu ...

  9. hihocoder#1333 : 平衡树·Splay2 (区间操作)

    题面: #1333 : 平衡树·Splay2 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:好麻烦啊~~~~~ 小Hi:小Ho你在干嘛呢? 小Ho:我在干活啊! ...

  10. 关于thinkphp5URL重写

    可以通过URL重写隐藏应用的入口文件index.php,下面是相关服务器的配置参考: [ Apache ] httpd.conf配置文件中加载了mod_rewrite.so模块 AllowOverri ...