成对的交换链表的节点 Swap Nodes in Pairs
为什么80%的码农都做不了架构师?>>>
问题:
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.
解决:
① 直接交换即可。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution { //4ms
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null) return head;
ListNode slow = head;
ListNode fast = head.next;
while(fast != null && fast.next != null && fast.next.next != null){//避免空指针异常
int tmp = slow.val;
slow.val = fast.val;
fast.val = tmp;
slow = fast.next;
fast = slow.next;
}
int tmp = slow.val;//处理最后两个数
slow.val = fast.val;
fast.val = tmp;
return head;
}
}
②
class Solution {//5ms
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode header = new ListNode(-1);
header.next = head;
ListNode cur = header;
ListNode slow = head;
ListNode fast = head.next;
while(slow != null && fast != null){
slow.next = fast.next;
fast.next = slow;
cur.next = fast;
cur = slow;
slow = slow.next;
if(slow != null)fast = slow.next;
}
return header.next;
}
}
转载于:https://my.oschina.net/liyurong/blog/1527880
成对的交换链表的节点 Swap Nodes in Pairs相关推荐
- C语言链表交换相邻节点,LeetCode 24--两两交换链表中的节点 ( Swap Nodes in Pairs ) ( C语言版 )...
题目描述 : 解题思路 : 分为两种情况处理 , 当要交换的节点是链表的前两个节点时 , 当交换的节点非前两个节点时 ; 代码如下 : /** * Definition for singly-lin ...
- 【重点】LeetCode 24. Swap Nodes in Pairs
LeetCode 24. Swap Nodes in Pairs 参考网址:http://www.cnblogs.com/grandyang/p/4441680.html 此题算是链表中比较考察细节的 ...
- 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- 5.18 优先队列(堆) 滑动窗口(二) 交换链表的节点
295. 数据流的中位数 最简单的思路暴力法,每次读入数据都进行排序 但是中位数只对中间的一个或两个数据感兴趣,其他数没有必要进行交换或者比较 https://leetcode-cn.com/prob ...
- Leetcode 24.两两交换链表的节点 (每日一题 20210624)
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表.你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换.示例 1:输入:head = [1,2,3,4] 输出:[2,1,4,3] 示例 ...
- leetcode24题:两两交换链表的节点
题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 输入:head = [1,2,3,4] 输出:[2,1, ...
- Leetcode 24. Swap Nodes in Pairs
不定期更新leetcode解题java答案. 采用pick one的方式选择题目. 本题很简单,大致翻译过来的意思是将单链表相邻节点互换节点位置.额外的要求是使用O(1)空间,以及不修改节点值,仅对节 ...
- Leetcode24.Swap Nodes in Pairs两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...
- [LeetCode]两两交换链表中的节点(Swap Nodes in Pairs)
题目描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能 ...
最新文章
- 常用的数据结构-数组
- 请填写红包接口调用ip_SOME/IP的车载网络应用
- java web方面杂志_环境方面比较好发的期刊_全球环境类最好的期刊_环境领域top期刊...
- SQL Server 2005中创建CLR存储过程
- inline函数包涵static变量,调用时是否会有多份拷贝
- 如何修改vs2008代码字体
- 【JS 逆向百例】转变思路,少走弯路,X米加密分析
- UI版式设计模板,这样做才高级!
- 3 photolemur 样式_macOS下支持RAW格式的照片编辑工具
- 中国近代史-蒋廷黻-笔记-第一章-剿夷与抚夷-第二节-英国人做鸦片买卖
- 8cm等于多少像素_PPT尺寸你们都设置成多少(我问的不是分辨率像素,而是长、高尺寸)?...
- ENVI_建模工具的使用——以“指数计算”批处理为例
- 湖北农商行计算机类笔试题,2019年湖北农商行笔试入门汇总提前知~
- 有效扩展:来自预训练和微调变换器的见解、rct.ai训练出5亿参数的BERT-X模型
- 【操作系统】操作系统极速入门
- 计算机毕业设计springcloud房产销售平台
- NLP中的数据增强方法
- 一文看懂麒麟9000:153亿晶体管,刷新5G速度,还有更强游戏体验,并且会“继续前行”...
- 计算机操作题蝴蝶效应,办公自动化上机操作测试题
- 雷神之锤冠军游戏角色高清Mac动态壁纸