当前位置:网站首页>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;
}
边栏推荐
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
- 从产品角度看 L2 应用:为什么说这是一个游乐场?
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- 态路小课堂丨如何为CXP光模块选择光纤跳线?
- 石墨文档打开文档时快速定位到上次写的位置
- Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
- 从脚本到剪辑,影像大师亲授的后期制作秘籍
- 建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
- LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
猜你喜欢

ViT结构详解(附pytorch代码)

Analysis of the implementation principle of UUID from the perspective of source code

负载均衡原理分析与源码解读

老板加薪!看我做的WPF Loading!!!
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板

How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize

Spss-多元回归案例实操

Flutter气泡框实现

Open Office XML 格式里如何描述多段具有不同字体设置的段落

gpu-admission 源码分析
随机推荐
Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
从脚本到剪辑,影像大师亲授的后期制作秘籍
LeetCode 445. 两数相加 II
A little self-deprecating deconstruction about farmers "code"
Centos7环境使用Mysql离线安装包安装Mysql5.7
中小规模网站架构
再有人问你分布式事务,把这篇扔给他
负载均衡原理分析与源码解读
HDU 4135: Co-prime (the principle of inclusion and exclusion)
如何使用工程仪器设备在线监测管理系统
HDU 6040 Hints of sd0061 (技巧)
单目操作符(含原码反码补码转换)
力扣练习——60 二叉搜索子树的最大键值和
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
Spss-多元回归案例实操
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
WeChat applet, global variables change in one place and the state in other places also changes.
Redis设计与实现
Module 9 - Designing an e-commerce seckill system
2022年裁员潮,失业程序员何去何从?