[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-> ...
随机推荐
- Putty连接虚拟机Centos出现:Network error:Connection refused的解决方法
转自:http://www.bcoder.cn/17251.html 我和很多人一样都是linux菜鸟,但是呢又对此很是感兴趣,不折腾就是不爽: 我下载了centos 6.4安装好在VMware 虚拟 ...
- ELK 日志管理系统,再次尝试记录
简介: 第二次尝试 ELK 记录... 工作流程: 1.客户端的 Logstash 将日志信息采集到之后传输给 Redis 做消息队列 2.然后服务端的 Logstash 将日志从 Redis 中取出 ...
- 面试-Android之java基础
1.HashMap是否为线程安全. 不安全的. 2.int[] a ={1,2,3,4}; int[]b =a ; b[0]=3; a[0]的值是改变的. 3.组合模式 安卓listview的不同ce ...
- IEnumerator & IEnumerable
[IEnumerator] 用于遍历一个对象,IEnumerator在System.Collections命名空间中. public interface IEnumerator { object Cu ...
- centos7 更新源 安装ifconfig
centos7最小化安装后,ifconfig是不可用的,可以使用ip addr或ip link查看网络信息. 更新源之前,先确定网络是否连通.我用的虚拟机,因为桥接受公司ip限制,换成了NAT模式,确 ...
- 解剖Nginx·自动脚本篇(7)类型相关脚本系列
1 auto/types/sizeof 该脚本的功能,是通过测试程序获知给定的ngx_type的大小. 1.1 显示提示信息 echo $ngx_n "checking for $ngx_t ...
- SpringBoot31 整合SpringJDBC、整合MyBatis、利用AOP实现多数据源
一.整合SpringJDBC 1 JDBC JDBC(Java Data Base Connectivity,Java 数据库连接)是一种用于执行 SQL 语句的 Java API,可以为多种关系数 ...
- 341. Flatten Nested List Iterator展开多层数组
[抄题]: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...
- table 合并行和列
table合并行列,以及拆分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- SqlServer性能瓶颈分析
SqlServer性能瓶颈分析 一.内存瓶颈分析--SQLServer:Buffer Manager SELECT TOP 312 * FROM sys.dm_os_performance_coun ...
