当前位置:网站首页>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 .
边栏推荐
- Word splitting problem
- [data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge
- [reading papers] deepface: closing the gap to human level performance in face verification. Deep learning starts with the face
- Professional database management software: Valentina Studio Pro for Mac
- Linked list: delete the penultimate node of the linked list
- js 解构赋值
- 如何挑选基金产品?什么样的基金是好基金?
- Laravel 权限导出
- Using binary heap to implement priority queue
- A wechat app for shopping
猜你喜欢

专业的数据库管理软件:Valentina Studio Pro for Mac

Huffman tree and its application

Why does it feel that most papers still use RESNET as the backbone network rather than densenet?
![[reading paper] generate confrontation network Gan](/img/88/950b47cac330f208c0c9e100f42b4f.jpg)
[reading paper] generate confrontation network Gan
![[data analysis and visualization] key points of data drawing 12- importance of chart notes](/img/9c/c610c6f9d08952aece97f788ae35a7.jpg)
[data analysis and visualization] key points of data drawing 12- importance of chart notes

Opencv 15 face recognition and eye recognition

Queuing theory, game theory, analytic hierarchy process
![[data analysis and visualization] key points of data mapping 7- over mapping](/img/ae/d4e251b37ec4857c99f738ca981092.jpg)
[data analysis and visualization] key points of data mapping 7- over mapping
![[data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge](/img/a8/468a0c4d4a009e155679898fac4b81.jpg)
[data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge

Data processing in detailed machine learning (II) -- Feature Normalization
随机推荐
专业的数据库管理软件:Valentina Studio Pro for Mac
Example 4 linear filtering and built-in filtering
Introduction to facial expression recognition system - Technical Paper Edition
Opencv 07, pixel read, change and bitmap write
專業的數據庫管理軟件:Valentina Studio Pro for Mac
Retrofit easy to use
js 解构赋值
Opencv 9 resize size change rotate rotate blur mean (blur)
Introduction to facial expression recognition system -- offline environment configuration
Linear, integer, nonlinear, dynamic programming
Professional database management software: Valentina Studio Pro for Mac
PCR validation of basic biological experiments in [life sciences]
How can intelligent safe power distribution devices reduce the occurrence of electrical fire accidents?
OneNote使用指南(一)
Redis multiple servers share one
too old resource version,Code:410
Opencvsharp4 pixel read / write and memory structure of color image and gray image
Graph theory, tree based concept
[common tools] pyautogui tutorial
Opencvshare4 and vs2019 configuration