[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了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...
随机推荐
- Windows 7下安装MySQL Server卡在Apply Security Settings的解决方案(转)
如果操作无效,请卸载MySQL Server后换一个位置安装 例如默认的是C:\Program Files\MySQL 安装时选Custom修改到D:\Program Files\MySQL试试 == ...
- React中如何实现模态框每次打开都是初始界面
问题描述如下 解决方案:每次点击打开模态框的时候为,当前模态框设置一个独立的key值,代码如下: /* * 上传文件的模块框控制 * */ showFileModal = () => { thi ...
- 第二十一章 授予身份及切换身份——《跟我学Shiro》
目录贴:跟我学Shiro目录贴 在一些场景中,比如某个领导因为一些原因不能进行登录网站进行一些操作,他想把他网站上的工作委托给他的秘书,但是他不想把帐号/密码告诉他秘书,只是想把工作委托给他:此时和我 ...
- linux查看物理cpu的核数,个数,逻辑cpu的个数
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cpuinfo| ...
- sql server 全角与半角字符转换
/****** SQL转换全角/半角函数 开始******/CREATE FUNCTION ConvertWordAngle ( @str NVARCHAR(4000), --要转换的字符串 @f ...
- Python爬取链家二手房源信息
爬取链家网站二手房房源信息,第一次做,仅供参考,要用scrapy. import scrapy,pypinyin,requests import bs4 from ..items import L ...
- laravel的定时任务
首先在laravel项目命令创建: php artisan make:command TestCommand 会在App\Console\Commands文件下看到TestCommand.php文件, ...
- python+unittest框架第三天unittest之分离测试固件和公共代码,跳过案例的执行
我们在时间工作中,会将整个项目的代码分别放置多个模块中去编写.方便后期项目维护,比如,我们的web项目可能有多个IP地址,每个IP地址代表不同的测试环境.测试环境与Bat环境或者验收环境等.这就需要我 ...
- Java 文件下载工具类
Java 文件下载工具类 import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static Logger logger = ...
- [转帖]阿里的JDK预热warmup过程
预热warmup过程 https://blog.csdn.net/wabiaozia/article/details/82056520 Jwarmup 原理是记录上一次运行时已经变成native co ...