[LeetCode 题解]:Swap Nodes in Pairs
前言
【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的更多相关文章
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- 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 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- LeetCode 024 Swap Nodes in Pairs
题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 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 ...
- [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 ...
- 【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 ...
- 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-& ...
- leetcode:Swap Nodes in Pairs
Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...
随机推荐
- [Z]牛人林达华推荐有关机器学习的数学书籍
1. 线性代数 (Linear Algebra): 我想国内的大学生都会学过这门课程,但是,未必每一位老师都能贯彻它的精要.这门学科对于Learning是必备的基础,对它的透彻掌握是必不可少的.我在科 ...
- 关于python中的多进程模块multiprocessing
python中的multiprocessing是一个多进程管理包,主要作用也就是提供多进程,而不是多线程,在其中用的比较多估计也就是Process和Pipe两个类,如下代码所示: #!/usr/bin ...
- Spring高级话题
Spring Aware 在实际项目中,你不可避免的要用到spring容器本身的功能资源,这时你的bean要意识到spring容器的存在,才能调用spring提供的资源.spring aware本来就 ...
- git之生成SSH key
git之生成SSH key SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定.利用 SSH 协议可以有效防止远程管理过程中的信 ...
- delphi IOS BLE 代理
代理方法 centralManagerDidUpdateState: peripheralManagerDidUpdateState:代理方法. centralManager:willRestoreS ...
- C++Primer笔记-----day06
================================================================day06=============================== ...
- 微信小程序开发注意点和坑集
开发(Tips) 避开频繁setData * 小程序端对于频繁的逻辑层和显示层的交互很不友好,特别是安卓机,与浏览器上js直接操作DOM不同,小程序通过逻辑更新显示层并不完全实时,开发者应避免出现 ...
- apt 查询软件
apt-cache search percona-server apt list percona-server-server-5.6
- web前端整套面试题(二)--今日头条面试题
12道单选,7道不定项选择,2道编程题 一.单选(12题) 1.[单选题]在HTML中,( )可以在网页上通过链接直接打开邮件客户端发送邮件. A.<a href=”telnet:ming.zh ...
- Apache Hive (五)DbVisualizer配置连接hive
转自:https://www.cnblogs.com/qingyunzong/p/8715250.html 一.安装DbVisualizer 下载地址http://www.dbvis.com/ 也可以 ...
