当前位置:网站首页>86. separate linked list
86. separate linked list
2022-06-30 05:31:00 【Grayson Zhang】
Title Description
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
Answer key (C++)
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* small = new ListNode(0);
ListNode* smallHead = small;
ListNode* large = new ListNode(0);
ListNode* largeHead = large;
while (head != nullptr) {
if (head->val < x) {
small->next = head;
small = small->next;
} else {
large->next = head;
large = large->next;
}
head = head->next;
}
large->next = nullptr;
small->next = largeHead->next;
return smallHead->next;
}
};

边栏推荐
- Use the code cloud publicholiday project to determine whether a day is a working day
- 领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
- Unity ugui text value suspended enlarged display add text background
- C. Divan and bitwise operations
- VFPBS上传EXCEL并保存MSSQL到数据库中
- Bev instance prediction based on monocular camera (iccv 2021)
- 【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)
- Revit二次開發---未打開項目使用面板功能
- E: Topic focus
- Unity animator does not clip animation to play animation in segments
猜你喜欢

Introduction to mmcv common APIs

【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)

C语言基础小操作

How to judge the quality of network transformer? What symptom is network filter transformer broken?

Responding with flow layout

Terminal convenient SSH connection

Unity shader flat shadow
![[learning notes] AssetBundle, xlua, hot update (use steps)](/img/59/9d9f31cfe55a908f2f0705e95ecc05.jpg)
[learning notes] AssetBundle, xlua, hot update (use steps)

mmcv常用API介绍

【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)
随机推荐
Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
Nestjs introduction and environment construction
Unity3d get screen width and height
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
Xi'an Jiaotong 21st autumn economics online homework answer sheet (III) [standard answer]
Terminal convenient SSH connection
uboot通过终端发送‘r‘字符读取ddr内存大小
Learning about functions QAQ
Shopping list--
/Path/to/ idiom, not a command
Introduction to Redux: initial experience of Redux
Use the code cloud publicholiday project to determine whether a day is a working day
PKCs 12:personal information exchange syntax v1.1 translation part I
mmcv常用API介绍
Promise知识点拾遗
Introduction to mmcv common APIs
Bev instance prediction based on monocular camera (iccv 2021)
Xijiao 21 autumn "motor and drive" online homework answer sheet (III) [standard answer]
遥感图像/UDA:Curriculum-Style Local-to-Global Adaptation for Cross-Domain Remote Sensing Image Segmentat
旋转框目标检测mmrotate v0.3.1入门