前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

1.题目描述

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.

2. 题意

给定一个链表,请将链表中任意相邻的节点互换,并返回链表的头部

要求:算法必须使用常量空间,并且不能改变节点的值,只能改变节点的位置。

3. 思路

步骤一:删除current->next

步骤二:将tmp插入到current之前

步骤三: 递归调用

注意递归的终止条件:

current!=NULL && current->next!=NULL //剩余的节点大于等于两个

4: 解法

ListNode *swapPairs(ListNode *head){
if(head==NULL || head->next==NULL) return head;
ListNode *pre=new ListNode(0);
pre->next = head;
ListNode *newHead =pre;
ListNode *current =head;
while(current && current->next){
//删除current->next
ListNode *tmp = current->next;
current->next = current->next->next;
//插入tmp
tmp->next = pre->next;
pre->next=tmp;
//向后递归
pre=current;
current=current->next;
}
return newHead->next;
}

作者:Double_Win

出处: http://www.cnblogs.com/double-win/p/3939649.html 

声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~

[LeetCode 题解]:Swap Nodes in Pairs的更多相关文章

  1. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  2. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  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. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

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

  5. LeetCode 024 Swap Nodes in Pairs

    题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

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

  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】Swap Nodes in Pairs (middle)

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

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

  10. leetcode:Swap Nodes in Pairs

    Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...

随机推荐

  1. Android复杂自定义Listview实现

    在Android中实现Listview对新人来说比较难以理解,本人看了若干文章后觉得可以使用以下思路来让新人更好理解(同时也做好记录,免得自己以后忘记). 可参考博客:http://cinderell ...

  2. SpringBoot核心

    1.基本配置 1.1入口类和@SrpingBootApplication SpringBoot通常有一个名为*Application的入口类,入口类里有一个main方法,这个main方法就是一个标准的 ...

  3. delphi 选择文件夹,路径选择,浏览文件夹

    选择文件夹,路径选择, 文件夹 资源管理器 推荐  SelectDirectory http://docwiki.embarcadero.com/Libraries/Seattle/en/Vcl.Fi ...

  4. 分环境部署SpringBoot日志logback-spring.xml

    springboot按照profile进行打印日志 log4j logback slf4j区别? 首先谈到日志,我们可能听过log4j logback slf4j这三个名词,那么它们之间的关系是怎么样 ...

  5. spring security的原理及教程

    spring security使用分类: 如何使用spring security,相信百度过的都知道,总共有四种用法,从简到深为:1.不用数据库,全部数据写在配置文件,这个也是官方文档里面的demo: ...

  6. 从零开始搭建k8s-20180301

    yum install -y yum-utils git etcd yum-config-manager --add-repo https://download.docker.com/linux/ce ...

  7. k8s podpreset 参数注入

    启动apiserver时,增加 参数 --runtime-config=settings.k8s.io/v1alpha1=true kind: PodPresetapiVersion: setting ...

  8. Uniform & Attribute & Varying

    [Uniform & Attribute & Varying] 顶点着色器的输入变量用关键字“attribute”来限定. 片段着色器的输入变量(它和顶点着色器的输出变量相对应)用关键 ...

  9. 【SPOJ - LCS2】Longest Common Substring II【SAM】

    题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...

  10. GitHub 上的十一款热门开源安全工具

    作为开源开发领域的基石,“所有漏洞皆属浅表”已经成为一条著名的原则甚至是信条.作为广为人知的Linus定律,当讨论开源模式在安全方面的优势时,开放代码能够提高项目漏洞检测效率的理论也被IT专业人士们所 ...