当前位置:网站首页>Linked lists: rearranging linked lists
Linked lists: rearranging linked lists
2022-06-13 02:45:00 【Zeng Qiang】
Rearrange the speed pointer of the linked list , Merge list
subject
https://leetcode-cn.com/problems/LGjMqU/
Enter a linked list , Rearrange the node order of the linked list . for example :
Their thinking
According to the meaning , Divide the list into two parts , The first half and the second half .
Reverse the second half of the list , Then merge with the first half of the linked list , You can get the rearranged linked list .
The specific process is divided into 3 Step :
1. Find the first half of the linked list . utilize Speed pointer , The quick pointer goes first every time 2 Step , In addition, slow down the pointer by one step , When the block pointer goes to the end , The slow pointer just reaches the middle node of the linked list .
2. Put the second half List reversal .
3. Merge The first half and the reverse second half are linked .
The specific code is as follows :
Code
/** * 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 void reorderList(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode slow = dummy;
ListNode fast = dummy;
// Find the last node in the first half .
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next;
if(fast.next != null) {
fast = fast.next;
}
}
ListNode temp = slow.next;
slow.next = null;
// Merge list
link(head, reverseList(temp), dummy);
}
private void link(ListNode node1, ListNode node2, ListNode head) {
//1 2 3
//5 4
ListNode pre = head; // The sentinel node , Used to merge linked lists .
while(node1 != null && node2 != null) {
ListNode temp = node1.next;
pre.next = node1;
node1.next = node2;
pre = node2;
node1 = temp;
node2 = node2.next;
}
if(node1 != null){
// When the linked list is an odd number , Judge the tail node of the first half .
pre.next = node1;
}
}
private ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while(cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
summary
The problem is to rearrange the linked list , The law can be found by observing the case list , You only need to find the middle node of the linked list with the help of the fast and slow pointer , Reverse the second half of the list , Merge the first half of the linked list , You can get the answer .
边栏推荐
- js 解构赋值
- redis 多个服务器共用一个
- redis
- 冲刺强基计划数学物理专题一
- CDN single page reference of indexbar index column in vant framework cannot be displayed normally
- Introduction to facial expression recognition system - Technical Paper Edition
- Rounding in JS
- Huffman tree and its application
- Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
- 02 optimize the default structure of wechat developer tools
猜你喜欢
[reading point paper] deeplobv3+ encoder decoder with Atlas separable revolution
[data analysis and visualization] key points of data mapping 7- over mapping
Data warehouse notes | 5 factors that need attention for customer dimension modeling
05 tabBar导航栏功能
Special topic I of mathematical physics of the sprint strong foundation program
Data processing in detailed machine learning (II) -- Feature Normalization
[data analysis and visualization] key points of data drawing 10- construction of legend
Basic principle of bilateral filtering
[reading papers] comparison of deeplobv1-v3 series, brief review
Opencvsharp4 handwriting recognition
随机推荐
Pycharm and Anaconda ultra detailed installation and configuration tutorial
PCR validation of basic biological experiments in [life sciences]
[data analysis and visualization] key points of data drawing 8- use of circular bar chart
Rounding in JS
[data analysis and visualization] key points of data drawing 11- precautions for radar chart
redis.conf总配置详解
Delphi implements adding a column of serial number to the CXGRID list
nn. Conv2d and nn Convtranspose2d differences
Introduction to facial expression recognition system -- offline environment configuration
Principle and steps of principal component analysis (PCA)
Ijkplayer source code - setting option 2
OpenCVSharpSample05Wpf
Find the number of permutations
Ijkplayer source code - remuxing
Special topic I of mathematical physics of the sprint strong foundation program
js多种判断写法
Ijkplayer source code - audio playback
vant实现移动端的适配
Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示
[reading papers] deepface: closing the gap to human level performance in face verification. Deep learning starts with the face