当前位置:网站首页>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;
}
边栏推荐
- LeetCode 445. 两数相加 II
- 如何使用工程仪器设备在线监测管理系统
- 快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
- Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
- Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
- OPNsense安装配置Zenarmor
- From the product dimension, why can't we fully trust Layer2?
- Where can I view the version record of WeChat applet submission review history?
- 从产品角度看 L2 应用:为什么说这是一个游乐场?
- LeetCode 82. 删除排序链表中的重复元素 II
猜你喜欢
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
Open Office XML 格式里如何描述多段具有不同字体设置的段落
A case of violent parameter tuning in machine learning
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
Analysis of the implementation principle of UUID from the perspective of source code
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
Redis设计与实现
一文带你搞懂中断按键驱动程序之poll机制
为什么Redis很快
Network sockets (UDP and TCP programming)
随机推荐
软件架构简介
LAXCUS分布式操作系统安全管理
使用.NET简单实现一个Redis的高性能克隆版(六)
WeChat applet, global variables change in one place and the state in other places also changes.
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
LeetCode 362. Design Hit Counter(计数器)
Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
基于UiAutomator2+PageObject模式开展APP自动化测试实战
一文带你搞懂中断按键驱动程序之poll机制
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
Where can I view the version record of WeChat applet submission review history?
力扣练习——59 从二叉搜索树到更大和树
自媒体爆款标题怎么写?手把手教你写热门标题
接口定义与实现
蔚来-软件开发工程师一面记录
The brave rice rice, does not fear the brush list of 】 list has a ring
阻塞 非阻塞 poll机制 异步
【勇敢饭饭,不怕刷题之链表】有序链表的合并
学长告诉我,大厂MySQL都是通过SSH连接的