当前位置:网站首页>leetcode 143. 重排链表
leetcode 143. 重排链表
2022-08-02 02:45:00 【henujolly】
class Solution {
public void reorderList(ListNode head) {
if (head == null) {
return;
}
ListNode mid = middleNode(head);
ListNode l2 = reverseList(mid);
mergeList(head, l2);
}
public void merge(ListNode a,ListNode b){
ListNode l1=a;
ListNode l2=b;
while(a!=null&&b!=null){
l1=a.next;
l2=b.next;
a.next=b;
a=l1;
b.next=a;
b=l2;
}
}
public void mergeList(ListNode l1, ListNode l2) {
ListNode l1_tmp;
ListNode l2_tmp;
while (l1 != null && l2 != null) {
l1_tmp = l1.next;
l2_tmp = l2.next;
l1.next = l2;
l1 = l1_tmp;
l2.next = l1;
l2 = l2_tmp;
}
}
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
}
边栏推荐
猜你喜欢
随机推荐
AcWing 1053. 修复DNA 题解(状态机DP、AC自动机)
790. 数的三次方根
BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
机器人领域期刊会议汇总
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
非稳压 源特电子 隔离电源模块芯片 5W VPS8504B 24V
【LeetCode】206.反转链表
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
mysql 查看死锁
Nacos源码分析专题(一)-环境准备
FOFAHUB使用测试
CASE2023
项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
简单的页面跳转活动
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
Chrome浏览器无法加载已解压的.crx文件的解决办法
Install mysql using docker
NAS和私有云盘的区别?1篇文章说清楚
架构:分布式任务调度系统(SIA-Task)简介
Oracle19c安装图文教程