[LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
描述
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
示例:
给定 1->2->3->4, 你应该返回 2->1->4->3.
说明:
你的算法只能使用常数的额外空间。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
思路
该题属于基本的链表操作题。
设置一个虚拟头结点
dummyHead设置需要交换的两个节点分别为
node1、node2,同时设置node2的下一个节点next
在这一轮操作中
将
node2节点的next设置为node1节点将
node1节点的next设置为next节点将
dummyHead节点的next设置为node2结束本轮操作
接下来的每轮操作都按照上述进行。
代码
public static ListNode swapPairs(ListNode listNode) {
if (listNode == null) {
return null;
}
ListNode head = new ListNode(-1);
head.next = listNode;
ListNode pHead = head;
while (pHead.next != null && pHead.next.next != null) {
ListNode node1 = pHead.next;
ListNode node2 = pHead.next.next;
ListNode next = node2.next;
node2.next = node1;
node1.next = next;
pHead.next = node2;
pHead = node1;
}
return head.next;
}
[LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)的更多相关文章
- 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】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 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(交换链表中每两个相邻节点)
题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...
- (链表 递归) 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 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...
- LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)
题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...
- [leetcode]24. Swap Nodes in Pairs交换链表的节点
感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...
随机推荐
- 使用 ServiceStack.Text 序列化 json
相信做 .net 开发的朋友经常会遇到 json 序列化这样的需要,今天发篇文章总结下自己使用 ServiceStack.Text 来序列化 json.它的速度比 Newtonsoft.Json 快很 ...
- Python编译出现错误SyntaxError: Non-ASCII character '\xe7' 时解决方法
转载个解决办法:https://blog.csdn.net/wangchao701123/article/details/57084244 转自https://blog.csdn.net/jim742 ...
- React16+配置打包目录
在学习react的时候,肯定最终都要用脚手架,对比了手写配置webpack(比较费劲).generator-react-webpack(打不开官方文档……),最终还是选择了react官方的create ...
- ASP.NET MVC4中的异步控制器
在抛弃了对.NET 3的支持之后, ASP.NET MVC 4 彻底拥抱了Task类库, 你不需要再蛋疼的给每个Action写两个方法, 也无需傻傻的手动对异步Action计数器增减了(AsyncMa ...
- sql语句中,取得schema中的所有表信息及表的定义结构
postgressql下'検索スキーマの中で.全てテーブルselect tablename from pg_tables where schemaname='test' mysql下'検索スキーマの中 ...
- Spring Boot代码生成
一.背景 在Java web开发中,虽然Spring boot已经帮助我们简化了很多工作,但项目中庞杂的业务仍然需要自己去编写较多的 entity,vo,Mapper,Service, Control ...
- qt qml中的Tabview使用心得
彩云之南的天是如此湛蓝,天上落下的水是如此清澈. 最近在qt5.5下使用TabView,如下. 1) currentIndex变量很好用,其对应当前被显示的tab,其值变化时还会触发onCurrent ...
- mui 打包发布ios 测试
1.首先在Hbuilder新建一个app项目,把你的代码放进来 2.在manifest.json里设置你想要的一切,图标,应用名,描述,入口页面等等等,然后再配置好你程序里需要用到的模块权限,按需配置 ...
- oracle 列合并成并用拼接符拼接 -- LISTAGG函数用法
==注:wm_concat(str1) 11g 后不支持使用== LISTAGG函数用法 select LISTAGG(name, ',') WITHIN GROUP (ORDER BY id) fr ...
- Hadoop的eclipse的插件是怎么安装的?
[学习笔记] 1)网上下载hadoop-eclipse-plugin-2.7.4.jar,将该jar包拷贝到Eclipse安装目录下的dropins文件夹下,我的目录是C:\Users\test\ec ...