当前位置:网站首页>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;
}
边栏推荐
- Some tips for using Unsafe
- 【勇敢饭饭,不怕刷题之链表】链表中有环的问题
- LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
- Emulate stm32 directly with proteus - the programmer can be completely discarded
- LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
- ViT结构详解(附pytorch代码)
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- Weilai-software development engineer side record
- AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
- Redis常用命令
猜你喜欢
随机推荐
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
项目部署、
力扣练习——59 从二叉搜索树到更大和树
推荐6个自媒体领域,轻松易上手
HDU 4135: Co-prime (the principle of inclusion and exclusion)
HDU 4135:Co-prime (容斥原理)
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
Nocalhost - 让云原生时代的开发更高效
From the product dimension, why can't we fully trust Layer2?
力扣练习——61 根据字符出现频率排序
LeetCode 445. 两数相加 II
HDU 4372:Count the Buildings (Stirling数)
程序员追求技术夯实基础学习路线建议
一文带你搞懂中断按键驱动程序之poll机制
Since the media hot style title how to write?Taught you how to write the title
使用.NET简单实现一个Redis的高性能克隆版(六)
Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
gpu-admission 源码分析
模块九 - 设计电商秒杀系统
老板加薪!看我做的WPF Loading!!!