当前位置:网站首页>30. Rearrange the linked list
30. Rearrange the linked list
2022-07-24 12:47:00 【Little happy】
The first 23 God
143. Rearrange the list
Medium difficulty 633 Switch to English to receive dynamic feedback
Given a single chain table L The head node of head , Single chain list L Expressed as :
L0 → L1 → … → Ln-1 → Ln
Please rearrange them to :
L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …
You can't just change the value inside the node , It needs to actually exchange nodes .
Example 1:

Input : head = [1,2,3,4]
Output : [1,4,2,3]
Example 2:

Input : head = [1,2,3,4,5]
Output : [1,5,2,4,3]
Algorithm analysis
1、 Use the speed pointer , Find the central node of the linked list , It's divided into two lists
2、 Reverse the linked list on the right
3、 Merge two linked lists
The question : Put the... Of a linked list kk Item and penultimate kk Items together .
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode middleNode(ListNode head){
ListNode fast = head;
ListNode slow = head;
while(fast!=null&&fast.next!=null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// List flip
public ListNode reverse(ListNode head){
if(head==null||head.next==null) return head;
ListNode tail = reverse(head.next);
head.next.next = head;
head.next = null;
return tail;
}
public ListNode merge(ListNode left,ListNode right){
ListNode dummy = new ListNode(0);
ListNode t = dummy;
while(left!=null&&right!=null){
t.next = left;
t = t.next;
left = left.next;
t.next = right;
t = t.next;
right = right.next;
}
if(left!=null){
t.next = left;
t = t.next;
left = left.next;
}
if(right!=null){
t.next = right;
t = t.next;
right = right.next;
}
return dummy.next;
}
public void reorderList(ListNode head) {
if(head==null) return;
ListNode middle = middleNode(head);
ListNode left = head;
ListNode right = reverse(middle.next);
middle.next = null;
head = merge(left,right);
}
}
边栏推荐
- regular
- ASP. Net core deployment Manual: 1. Deployment Basics
- Is it safe to contact the account manager online to open a fund account?
- Is it safe to open an account on Oriental Fortune online? Is there a threshold for opening an account?
- 突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战
- Why has API strategy become a magic weapon for enterprises' digital transformation?
- cookie
- 6-16漏洞利用-rlogin最高权限登陆
- SSM online rental and sales platform multi city version
- setAttribute、getAttribute、removeAttribute
猜你喜欢

Everything about native crash
向勒索病毒说不,是时候重塑数据保护策略
![[function test] test of the project - login and post function](/img/64/c9bbf34964622f4f013b1184eeb1e0.jpg)
[function test] test of the project - login and post function

基于matlab的声音识别
Efficientformer: lightweight vit backbone

3.实现蛇和基本游戏界面

高速成长的背后,华为云乌兰察布数据中心的绿色之道
![[rust] reference and borrowing, string slice type (& STR) - rust language foundation 12](/img/48/7a1777b735312f29d3a4016a14598c.png)
[rust] reference and borrowing, string slice type (& STR) - rust language foundation 12

Node takes effect after using NVM to install under Windows system, but NPM does not take effect

SSM在线校园相册管理平台
随机推荐
Use typeface to set the text font of textview
Say no to blackmail virus, it's time to reshape data protection strategy
Custom scroll bar
This is how the permission system is designed, yyds
Unity rotation test
向勒索病毒说不,是时候重塑数据保护策略
Ah, I am uncle card space
突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战
如何使用autofs挂载NFS共享
Promise
Seckill implementation diagram
sql的where+or的用法丢失条件
从零实现深度学习框架——再探多层双向RNN的实现
English grammar_ Indefinite pronouns - Overview
Why does 2.tostring() report an error
SQL JOIN 入门使用示例学习左连接、内连接、自连接
基于Kubernetes v1.24.0的集群搭建(三)
Okaleido tiger NFT is about to log in to binance NFT platform
English语法_不定代词 - 概述
Reserved instances & Savings Plans