当前位置:网站首页>LeetCode 86. 分隔链表
LeetCode 86. 分隔链表
2022-08-10 11:09:00 【水菜笔】
原题网址:https://leetcode.cn/problems/partition-list/submissions/
给一个链表,和一个定值;要求小于这个定值的排在最前面剩下的在排在一起;
用两个链表;一个保存小于定值的节点,一个保存剩下的链表;
public ListNode partition(ListNode head, int x) {
// 用两个链表一个保存小于x值的一个保存剩下的
ListNode lessNode = new ListNode(-1);
ListNode less = lessNode;
ListNode moreNode = new ListNode(-1);
ListNode more = moreNode;
ListNode cur = head;
while(cur != null) {
if(cur.val < x) {
ListNode temp = cur;
less.next = cur;
less = less.next;
cur = cur.next;
temp.next = null;
} else {
ListNode temp = cur;
more.next = cur;
more = more.next;
cur = cur.next;
temp.next = null;
}
}
less.next = moreNode.next;
return lessNode.next;
}
边栏推荐
- Licking Exercise - 63 Find all anagrams in a string
- POJ 1026 Cipher (Permutation Groups)
- gpu-admission 源码分析
- Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
- Spss-多元回归案例实操
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- HDU 6040 Hints of sd0061 (技巧)
- 【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
- 为什么Redis很快
- 力扣练习——62 有效的数独
猜你喜欢

StoneDB Document Bug Hunting Season 1

A case of violent parameter tuning in machine learning

石墨文档打开文档时快速定位到上次写的位置

基于UiAutomator2+PageObject模式开展APP自动化测试实战

Since the media hot style title how to write?Taught you how to write the title

From the product dimension, why can't we fully trust Layer2?

孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感

LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
![[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists](/img/06/9d49fc99ab684f03740deb2abc38e2.png)
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists

关于振弦采集模块及采集仪振弦频率值准确率的问题
随机推荐
HDU 6040 Hints of sd0061 (技巧)
从源码角度分析UUID的实现原理
【机器学习】浅谈正规方程法&梯度下降
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
从脚本到剪辑,影像大师亲授的后期制作秘籍
使用JMeter进行MySQL的压力测试
CPU多级缓存与缓存一致性
振弦传感器及核心VM系列振弦采集模块
力扣练习——64 最长和谐子序列
为什么Redis很快
3款不同类型的自媒体免费工具,有效提高创作、运营效率
接口定义与实现
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Open Office XML 格式里如何描述多段具有不同字体设置的段落
不止跑路,拯救误操作rm -rf /*的小伙儿
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?