当前位置:网站首页>LeetCode143:重排链表
LeetCode143:重排链表
2022-08-04 21:34:00 【斯沃福德】
题目:
思路:双指针
- 由于链表没有索引,所以使用List先将节点依次装入;
- 根据题目要求的顺序,想到双指针 ! i 为最左, j 为最右,每次用 i 指向 j ,即为题目顺序!
指针 i 和 j 每次同时向中间移动,直到 i > j 则停止;
但是每一轮还需要将 j 指向 i+1,以连接本轮的 j 和下一轮的 i; - 注意:最后以一定要让末尾节点指向 null,否则会成环!
由于每一轮最后 i++, j-- ,所以末尾元素的索引是 i 而不是 j;
class Solution {
public void reorderList(ListNode head) {
ArrayList<ListNode> list=new ArrayList<>();
ListNode curr=head;
// 存
while(curr!=null){
list.add(curr);
curr=curr.next;
}
// 双指针!
int i=0;
int j=list.size()-1;
// 修改
while(i<j){
list.get(i).next=list.get(j);
if(i+1<j){
list.get(j).next=list.get(i+1);
}
i++;
j--;
}
// 最后要将i指针的元素指向null !!! 否则成环!
list.get(i).next=null;
}
}
边栏推荐
- Unknown point cloud structure file conversion requirements
- Develop your own text recognition application with Tesseract
- PowerCLi 导入License到vCenter 7
- Arduino 电机测速
- LayaBox---TypeScript---Problems encountered at first contact
- [QT] Implementation of callback function
- js data type, throttling/anti-shake, click event delegation optimization, transition animation
- C language knowledge (1) - overview of C language, data types
- JdbcTemplate概述和测试
- 1319_STM32F103串口BootLoader移植
猜你喜欢

数电快速入门(三)(卡诺图化简法的介绍)

In action: 10 ways to implement delayed tasks, with code!

零基础都能拿捏的七夕浪漫代码,快去表白或去制造惊喜吧

Win11如何开启Telnet客户端?
![[larave]关于laravel使用form submit()不能获取值问题](/img/2f/a7a17b1497390509edd6d096e8e473.png)
[larave]关于laravel使用form submit()不能获取值问题

Chapter7 : Network-Driven Drug Discovery

DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基一种疏水18碳磷脂

buu web

EasyGBS接入最新版海康摄像头后无法传递告警信息该如何解决?

Unknown point cloud structure file conversion requirements
随机推荐
1319_STM32F103串口BootLoader移植
Dotnet using WMI software acquisition system installation
AtCoder Beginner Contest 262 D - I Hate Non-integer Number
Chapter7 : Network-Driven Drug Discovery
模拟对抗之红队免杀开发实践
AXI interface application of Zynq Fpga image processing - the use of axi_lite interface
Go----Go 语言基础之标识符、关键字、命名规范、变量、常量
传奇服务器需要什么配置?传奇服务器租用价格表
Is the International Project Manager PMP certificate worth taking?
LayaBox---TypeScript---structure
88. (the home of cesium) cesium polymerization figure
Analysis and treatment of Ramnit infectious virus
Flutter 实现背景图片毛玻璃效果
LeetCode: 406. 根据身高重建队列
【CC3200AI 实验教程 1】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-开发环境搭建
Spss-系统聚类手算实操
知识分享|如何设计有效的帮助中心,不妨来看看以下几点
C语言知识大全(一)——C语言概述,数据类型
openresty lua-resty-template页面静态化
【QT】回调函数的实现