当前位置:网站首页>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);
}
}
边栏推荐
- Symbol
- 1.4.1 many, many symbols (cin/cout unbinding, O3 optimization)
- It is difficult for Chinese consumers and industrial chains to leave apple, and iPhone has too much influence
- 猿人学第五题
- Buckle exercise - 35 combination sum II
- Buckle practice - 31 effective tic tac toe games
- for mysql
- Slow motion animation, window related data and operations, BOM operations [DOM (V)]
- 02 linear structure 2 multiplication and addition of univariate polynomials (linked list solution)
- How to render millions of 2D objects smoothly with webgpu?
猜你喜欢

Wechat applet generates QR code
![Error: [synth 8-439] module 'xxx' not found not found error solution](/img/47/bb03cc26e254332bf815c80bafb243.png)
Error: [synth 8-439] module 'xxx' not found not found error solution

QT based software framework design

Get the current month and year and the previous 11 months

Installation and deployment of ansible

Promise

基于matlab的声音识别

Teach you how to use power Bi to realize four kinds of visual charts

Solutions to problems in IE6 browser

Cluster construction based on kubernetes v1.24.0 (I)
随机推荐
Custom scroll bar
微信小程序生成二维码
6-16 vulnerability exploitation -rlogin maximum permission login
Symbol
猿人学第六题
【C语言】详细的文件操作相关知识
Case summary of SSH service suddenly unable to connect
New applications of iSCSI and separation of storage services of NFS
Taishan Office Technology Lecture: layout difficulties of paragraph borders
Leetcode's 302 weekly rematch
向勒索病毒说不,是时候重塑数据保护策略
Is it safe for Huatai Securities to open a remote account? Is there any guarantee?
Getting started with SQL join use examples to learn left connection, inner connection and self connection
Is it safe to contact the account manager online to open a fund account?
Try... Finally summary
How to realize the function of grabbing red envelopes in IM system?
for mysql
突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战
Localstorage
Installation and deployment of ansible