leetcode 链表 Partition List
Partition List
Total Accepted: 19761 Total
Submissions: 73252My Submissions
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
题意:依据给定值x划分链表。使得比x小的值排在前面。要求保持元素原先的相对位置。
思路:
维护两个链表,一个是比x小的节点的链表l1,还有一个是大于等于x的节点的链表l2
最后把l1的尾部接到l2的头部
复杂度:时间O(n)。空间O(1)
ListNode *partition(ListNode *head, int x) {
ListNode less(-1), larger_equal(-1);
ListNode *less_ptr = &less, *larger_equal_ptr = &larger_equal;
for (ListNode *cur = head; cur != NULL; cur = cur->next)
{
if(cur->val < x){
less_ptr = less_ptr->next = cur;
}else{
larger_equal_ptr = larger_equal_ptr->next = cur;
}
}
less_ptr->next = larger_equal.next;
larger_equal_ptr->next = NULL;
return less.next;
}
leetcode 链表 Partition List的更多相关文章
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- [LeetCode] [链表] 相关题目总结
刷完了LeetCode链表相关的经典题目,总结一下用到的技巧: 技巧 哑节点--哑节点可以将很多特殊case(比如:NULL或者单节点问题)转化为一般case进行统一处理,这样代码实现更加简洁,优雅 ...
- Leetcode链表
Leetcode链表 一.闲聊 边学边刷的--慢慢写慢慢更 二.题目 1.移除链表元素 题干: 思路: 删除链表节点,就多了一个判断等值. 由于是单向链表,所以要删除节点时要找到目标节点的上一个节点, ...
- [LeetCode] 86. Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- LeetCode 86. 分隔链表(Partition List)
86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...
- LeetCode 86. Partition List 划分链表 C++
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [leetcode]86. Partition List划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【LeetCode】Partition List ——链表排序问题
[题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...
- [LeetCode]86. Partition List分离链表
/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...
随机推荐
- Excel操作之VLOOKUP
https://support.office.com/en-us/article/VLOOKUP-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1 Use V ...
- js如何实现简繁体互转
js如何实现简繁体互转 一.总结 一句话总结:其实无论是简体还是繁体,都是在显示端(前端),其实所有的我只用动js就好了,没必要动php. 当然,后端也可以做前端的事情,只是麻烦了点(要多通信两次,第 ...
- 41.AngularJS 服务(Service)
转自:https://www.cnblogs.com/best/tag/Angular/ 什么是服务? 在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用. A ...
- [HNOI2012] 永无乡 解题报告 (splay+启发式合并)
题目链接:https://www.luogu.org/problemnew/show/P3224#sub 题目: 题目大意: 维护多个联通块,没有删除操作,每次询问某一联通块的第k大 解法: 维护联通 ...
- Cordova Android项目如何做代码混淆
我想修改build.gradle配置 可是这个文件明确写了// GENERATED FILE! DO NOT EDIT!可是还是试了试: if (cdvReleaseSigningProperties ...
- python 爬虫简介
初识Python爬虫 互联网 简单来说互联网是由一个个站点和网络设备组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现 ...
- 03016_DBCP连接池
1.连接池概述 (1)用池来管理Connection,这样可以重复使用Connection: (2)有了池,所以我们就不用自己来创建Connection,而是通过池来获取Connection对象: ( ...
- 洛谷—— P1877 [HAOI2012]音量调节
https://www.luogu.org/problem/show?pid=1877#sub 题目描述 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都需要 ...
- hibernate session.save()和session.persist()的区别
save()需要返回一个Serialzable的实现类,因此执行这个方法时会马上插入到数据库 而persist()不会立即插入到数据库. "当我们封装一个长会话流程的时候,persist() ...
- ORA-12514: TNS: 监听程序当前无法识别连接描写叙述符中请求的服务
不指定数据库能够正常连接: 指定数据库和使用PL/SQL Developer都出现错误: 在此说明一下我的环境:Oralce装的是64位的在使用PL/SQL Developer时曾出现过初始化错误,解 ...