前言

 

【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. C++何时使用引用

  2. 微信登录失败,redirect_uri域名与后台配置不一致,错误代码10003

    微信登录失败,redirect_uri域名与后台配置不一致,错误代码10003 1 先检查网页的授权域名  不要带http:// 2 检查下自己的appid是否正确 我换了appid没上传,多花了时间 ...

  3. C# 本地文件夹上传至网络服务器中(待续)

    一.文件的上传参考 思想,C#FTP上传 /// <summary> /// 上传 /// </summary> /// <param name="filena ...

  4. linux系统构架 - LB集群之LVS介绍

    LB 集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有 nginx.lvs.keepalived ,商业的硬件负载设备 F5.Netscale. LB ...

  5. Server_id 冲突导致 IO 等待故障

    问题描述: 线上添加新的 MySQL Slave 后,服务器异常. 1.show processlist; Queueing master event to the relay log Reconne ...

  6. python安装库(Windows下)

    首先确保安装了pip,并且pip也加入了系统path路径: pip下载:https://pypi.python.org/pypi/pip#downloads 下载Python对应的包:(http:// ...

  7. kittle 使用心得

    1,字体编码格式: 解析excel表格时,出现乱码,两处修改:1, 2,

  8. os.path.dirname( __ file __ ) 2018/6/2

    os.path.dirname( __ file __ ) 2018/6/2 该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py import os #该文件所在位 ...

  9. Vertex Lit

    [Vertex Lit] Vertex Lit path generally renders each object in one pass, with lighting from all light ...

  10. 一款比较好用的JS时间控件-laydate

      官方讲解:http://laydate.layui.com/ 具体的内容请看官方讲解,此处仅说名应用: 1.在jsp或html或其他中引入laydate.js <script src=&qu ...