当前位置:网站首页>LeetCode 0086. Separate linked list
LeetCode 0086. Separate linked list
2022-06-27 06:04:00 【Tisfy】
【LetMeFly】86. Separate the list
Force button topic link :https://leetcode.cn/problems/partition-list/
Give you a list of the head node head And a specific value x , Please separate the linked list , Make all Less than x All of the nodes appear in Greater than or equal to x Before the node .
You should Retain The initial relative position of each node in the two partitions .
Example 1:

Input :head = [1,4,3,2,5,2], x = 3 Output :[1,2,2,4,3,5]
Example 2:
Input :head = [2,1], x = 2 Output :[1,2]
Tips :
- The number of nodes in the linked list is in the range
[0, 200]Inside -100 <= Node.val <= 100-200 <= x <= 200
Method 1 : Double pointer
We only need to base the original linked list on “ Is less than x” The rules of are divided into two linked lists , Then merge the two linked lists .
First define two new header nodes , Traverse the original linked list , Add each node to the new corresponding linked list .
Last , hold “ Less than x The linked list of ” Of the last node next Point to “ Greater than or equal to x The linked list of ” The first node of , hold “ Greater than or equal to x The linked list of ” Of the last node next empty , return “ Less than x The linked list of ” The first node of the .
See code Notes for detailed implementation .
- Time complexity O ( N ) O(N) O(N), among N N N Is the number of nodes in the original linked list
- Spatial complexity O ( 1 ) O(1) O(1), Only constant nodes are required ( To add a node to the end of the linked list, you only need to modify next The pointer , No extra space is required )
AC Code
C++
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* le = new ListNode, *ge = new ListNode; // Less than x The linked list of 、 Greater than or equal to x The linked list of
ListNode* p1 = le, *p2 = ge; // Two pointers
while (head) {
// Traverse the original linked list
if (head->val < x) {
p1->next = head; // Add to “ Less than x The linked list of ” Back
p1 = p1->next; // Pointer backward
}
else {
p2->next = head; // // Add to “ Greater than or equal to x The linked list of ” Back
p2 = p2->next;
}
head = head->next;
}
p1->next = ge->next; // “ Less than x The linked list of ” The last node of points to “ Greater than or equal to x The linked list of ” The first node of ( The header node is empty , No value stored , So point to ge->next)
p2->next = nullptr; // “ Greater than or equal to x The linked list of ” Of the last node next empty
return le->next; // return “ Less than x The linked list of ” The first node of
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125467594
边栏推荐
- Nlp-d62-nlp competition d31 & question brushing D15
- [cocos creator 3.5.1] addition of coordinates
- Configuring the help class iconfiguration in C # NETCORE
- Sqlsever 字段相乘后保留2位小数
- [FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design
- js实现双向数据绑定
- IAR Systems全面支持芯驰科技9系列芯片
- C language implementation timer
- Codeforces Round #802 (Div. 2)
- QListWidgetItem上附加widget
猜你喜欢

Asp. Net core6 websocket simple case

【QT小作】使用结构体数据生成读写配置文件代码

Codeforces Round #802 (Div. 2)

JVM的垃圾回收机制

创建一个基础WDM驱动,并使用MFC调用驱动

Leetcode99 week race record

线程间等待与唤醒机制、单例模式、阻塞队列、定时器

427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)

leetcode298周赛记录

Two position relay hjws-9440
随机推荐
MATLAB快速将影像的二维坐标转换为经纬度坐标
[cocos creator 3.5.1] addition of coordinates
JVM对象组成和存储
The SCP command is used in the expect script. The perfect solution to the problem that the SCP command in the expect script cannot obtain the value
[FPGA] design and implementation of frequency division and doubling based on FPGA
【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
WebRTC系列-網絡傳輸之7-ICE補充之提名(nomination)與ICE_Model
多线程基础部分Part 1
IP网络通信的单播、组播和广播
Jump details of item -h5 list, and realize the function of not refreshing when backing up, and refreshing when modifying data (record scroll bar)
310. minimum height tree
30个单片机常见问题及解决办法!
Go日志-Uber开源库zap使用
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
代码即数据
Open the door small example to learn ten use case diagrams
JS to implement bidirectional data binding
Configuration of vscode korofileheader
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
Unity中跨平臺獲取系統音量