当前位置:网站首页>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
边栏推荐
猜你喜欢

Program ape learning Tiktok short video production

yaml文件加密

Formation and release of function stack frame

爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)

免费的 SSH 和 Telnet 客户端PuTTY

JVM常用指令

JVM的垃圾回收机制

Asp. Net core6 websocket simple case

Small program of C language practice (consolidate and deepen the understanding of knowledge points)

Using domain name forwarding mqtt protocol, pit avoidance Guide
随机推荐
数据库-索引
项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
Codeforces Round #802 (Div. 2)
Netease cloud music params and encseckey parameter generation code
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
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
我对于测试团队建设的意见
LeetCode-515. 在每个树行中找最大值
Opencv实现对象跟踪
C Primer Plus 第11章_字符串和字符串函数_代码和练习题
【Cocos Creator 3.5.1】input.on的使用
leetcode298周赛记录
Configuring the help class iconfiguration in C # NETCORE
JVM整体结构解析
代码即数据
Codeforces Round #802 (Div. 2)
【QT小点】QT下载链接
openresty使用文档
OpenCV的轮廓检测和阈值处理综合运用
Create a basic WDM driver and use MFC to call the driver