Leetcode 24——Swap Nodes in Pairs
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.
分析:调换每各pair中的两个node的位置,并返回新的首节点,不能直接改变node的值,而是要调整node的位置。这里要注意描述,调整pair中两个node的位置,返回新的首节点,一开始我只是调整完并没有返回新的首节点,结果几次报错都不知道为什么,明明调试出来的结果是正确的,看清题目很重要。其实这个题目的思路很简单,找到要调换的位置,然后调换一下就可以了,注意临界条件。先上我的代码。
public ListNode swapPairs(ListNode head) {
if (head == null|| head.next == null)
return head;
ListNode finalHead=head.next;
int index = 1;
ListNode headBefore = null;
ListNode headBeforeBefore = null;
while (head!=null&&(head.next != null||(head.next==null&&headBefore.next!=null))) {
if (index % 2 == 0) {
//调换pair中的位置
if(headBeforeBefore!=null)
headBeforeBefore.next=head;
headBefore.next=head.next;
head.next=headBefore;
//调整新的head headB headBB的指代
headBeforeBefore=head;
head=headBefore.next;
index++;
}else {
//调整新的head headB headBB的指代
headBeforeBefore = headBefore;
headBefore = head;
head=head.next;
index++;
}
}
return finalHead;
}
声明了两个节点,一个headB,一个headBB,代表head的父节点与head的祖父节点,然后index计数,每到2进行调换。其实后来想了一下,可以不用这样,在进行调换完成之后,直接定位到下一次要调换的位置,然后操作即可,这样会更快一点。每次调换之后,设置新的head、headB、headBB位置。
A掉之后看了别人的代码,简洁明了,用了递归。
public static ListNode swapPairs(ListNode head) {
if(head==null||head.next==null) {
return head;
}
ListNode n=head.next;
head.next=swapPairs(head.next.next);
n.next=head;
return n;
}
思路是设置完自身后,调用下一个要调换位置节点的方法。
Leetcode 24——Swap Nodes in Pairs的更多相关文章
- 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] 24. Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
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
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交换节点对
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. For example, Given 1->2-& ...
随机推荐
- FtpHelper ftp操作类库
FtpHelper ftp操作类库 using System; using System.Collections.Generic; using System.Linq; using System.Te ...
- html中的块元素和内联元素的区别
一.定义 块元素一般都从新行开始,它可以容纳内联元素和其他块元素,可设置高度.宽度和边距等. 内联元素一般都是基于语义级的基本元素,它只能容纳文本或其他内联元素,主要特点是:和其他元素位于同一行上,高 ...
- C#图解教程 第十六章 转换
转换 什么是转换隐式转换显式转换和强制转换 强制转换 转换的类型数字的转换 隐式数字转换溢出检测上下文 1.checked和unchecked运算符2.checked语句和unchecked语句 显式 ...
- Python机器学习介绍(Python Machine Learning 中文版)
Python机器学习 机器学习,如今最令人振奋的计算机领域之一.看看那些大公司,Google.Facebook.Apple.Amazon早已展开了一场关于机器学习的军备竞赛.从手机上的语音助手.垃圾邮 ...
- 【洛谷1541】【CJOJ1087】【NOIP2010】乌龟棋
题面 Description 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌 ...
- [BZOJ2326] [HNOI2011] 数学作业 (矩阵乘法)
Description Input Output Sample Input Sample Output HINT Source Solution 递推式长这样:$f[n]=f[n-1]*10^k+n$ ...
- NOIP2017划水记
考完好久后才想起来写个总结 Day0 在Luogu上码了几道模板题 然后就忍不住和机房的人开始颓了本来说要复习一下的 将军棋到CS,一直在颓CS打的还不错 晚上开了几把后就昏昏沉沉睡了 Day1 好紧 ...
- Redis进阶实践之十八 使用管道模式加速Redis查询
一.引言 学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中批量的增加数据,肯定是大批量的,为了这主题,我从新找起了解决方案.目前 ...
- Wp-UserAgent——让WordPress在评论后面加上浏览器和操作系统信息
在很多的博客网站都看到过在评论的后面显示了浏览器和操作系统的信息,网上也用过一些插件,但是都不是很好看,有一次在一个网页上看见了这个评论后面不仅显示了浏览器和操作系统的图片,还有文字信息, 感觉不错, ...
- Error400
关于Error400,研究了几天终于弄明白了.不是FQ的问题,也不是DNS污染的问题.之前网上很多帖子说Error400可以通过删除 cookies来解决.但是其实这个并不管用.也就是说.原因并不是由 ...