当前位置:网站首页>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;
}
}
边栏推荐
- VPS8505 微功率隔离电源隔离芯片 2.3-6V IN /24V/1A 功率管
- EFCore 反向工程
- cocos中使用async await异步加载资源
- NIO's Sword
- C#测试项目中属性的用法
- ALCCIKERS Shane 20191114
- 树链剖分-
- OperatingSystemMXBean to get system performance metrics
- 2022牛客多校三_F G
- The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
猜你喜欢
随机推荐
Remember a gorm transaction and debug to solve mysql deadlock
ReentrantLock工作原理
ALCCIKERS Shane 20191114
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
OC中new和init的区别
svm.SVC application practice 1--Breast cancer detection
BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
789. 数的范围
使用DBeaver进行mysql数据备份与恢复
Recursively check if a configuration item has changed and replace it
OC中成员变量,实例变量和属性之间的区别和联系
FOFAHUB使用测试
2022 NPDP take an examination of how the results?How to query?
ApiFox 基本使用教程(浅尝辄止,非广)
【LeetCode】94.二叉树的中序遍历
cocos中使用async await异步加载资源
字典常用方法
qt点云配准软件
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
【LeetCode】20.有效的括号









