leetcode 86. Partition List(分割链表)

主升浪潜伏主图指标公式 提前潜伏,持仓待涨,通达信炒股指标公式 浙江世宝,第一次出现信号2022年6月1日,当日冲高回落,收涨3.48%,随后涨涨突破前高,回踩后出现第二次主升潜伏信号,时间2022年6月13日,当日收涨9.46%,后市连续涨停,走出12连板行情。九安医疗,第一次出现信号,黄色文字,当日2021年11月1日,涨幅0.19%,第二次出现信号一字涨停,没机会买进,第三次是T字,需要手快,后市连续上涨,接近20倍的大牛股。启明信息,突破区间后的回踩转涨位置出现信号,时间2021年11月29日,当日涨幅1.14%,次日爆发,波段涨幅152.65%。 阅读详情

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.

Example:

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5

题目是给一个链表,一个数x,让排列链表,>=x的元素排在<x的元素后面,而且<x的部分和>=x的部分的元素顺序要和原链表中一致

思路:

方法一:交换节点

找到第一个>=x的元素,然后它后面的<x的元素都移到这个元素的前一个位置

注意不是移到固定的pre指针的后面,那样会变成倒序,
是保留原有的顺序,所以要不断更新pre节点。

 public ListNode partition(ListNode head, int x) {
     if(head == null || head.next == null) return head;
     
     ListNode newHead = new ListNode();
     newHead.next = head;
     
     ListNode pre = newHead;
     ListNode tmp = head;
     ListNode preTmp = newHead;
     
     //不能是tmp.next != null
     //要把所有<x都跨过去,如果整个链表都<指定的x,直接可以让tmp到达最后的null,
     //进而下一步也可跳过,如果停在最后的node,下一步就会产生交换,丢失掉最后的node
     //可以用[1,1] 2测试
     while(tmp != null) {
         if(tmp.val >= x) break;
         
         pre = pre.next;
         tmp = tmp.next;
         preTmp = preTmp.next;
     }
     
     while(tmp != null) {
         if(tmp.val < x) {
             preTmp.next = tmp.next;
             tmp.next = pre.next;
             pre.next = tmp;
             pre = pre.next;
             tmp = preTmp.next;
         } else {
             preTmp = preTmp.next;
             tmp = tmp.next;
         }
     }
     return newHead.next;
 }

方法二:保存节点值

用两个链表(比list快)分别保存 < x的值和 >= x 的值。
然后把这两个链表连接起来即可。

public ListNode partition(ListNode head, int x) {
    if(head == null) return head;
    ListNode small = new ListNode();
    ListNode large = new ListNode();

    ListNode sCur = small;
    ListNode lCur = large;

    while(head != null){
        if(head.val<x){
            sCur.next = head;
            sCur = sCur.next;
        }
        else{
            lCur.next = head;
            lCur = lCur.next;
        }
        head=head.next;

    }
    sCur.next = large.next;
    lCur.next=null;
    return small.next;
}
Vue2 - 最新详细实现接入天地图详细配置流程及示例代码教程,提供获取用户当前定位的城市名称及省市区数据、精准经纬度信息、IP属地定位、地图搜索标点等示例,解决天地图跨域报错/页面白屏/定位不准问题 vue2,nuxt2,天地图,使用教程,vue接入国家天地图,定位api,ip属地定位,获取用户当前位置数据,省市区,地图不显示,白屏,报错,vue2如何使用天地图,地图选点,cors跨域,白名单设置了还是不行,vue项目调用天地图api实现定位,Vue2中使用天地图,详细教程,天地图申请密钥key教程,经纬度,经纬度不准确,一直定位北京,定位在北京不准怎么办,nuxt项目天地图,网站,网页,h5页面,地图显示不全,获取当前定位,如何使用天地图,拿到客户ip地址定位城市名称,距离计算,实时搜索地址逆解析,天 阅读详情

相关推荐

2018南京大学计算机夏令营机试

1. Count number of binary strings without consecutive 1’s Given a positive integer n(3≤n≤90), count all possible distinct binary strings of length n such that there are no consecutive 1's . Examples...

climber16的博客 1万+

86. Partition List(分隔链表)两种解法(C++ & 注释)

86. Partition List(分隔链表)1. 题目描述2. 迭代(Iteration)2.1 解题思路2.2 实例代码3. 递归(Recursion)3.1 解题思路3.2 实例代码4. 参考资料 1. 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。 你应当保留两个分区中每个节点的初始相对位置。 示例: 输入: head = 1->4->3->2->5->2, x = 3 输出: 1->2-&gt

主要研究图形学相关领域 692

Eplan P8 API二次开发--插入符号(Symbol)功能(Function)并定义属性

1、目的 使用Eplan API在页中自动插入某个符号并按约定规则自动设置好属性 2. 步骤 一、基础信息 比如要在Eplan中自动插入一个电机的符号并定制属性,我们先要确定要插入的符号名,可以在Eplan中获取如下符号(Symbol)信息 可以知道我们待插入的符号名称为M3_1, 接下来,使用Visual Studio建立类库项目,并引用Eplan Api库 二、获取符号变量(SymbolVariant) 在Eplan中,当一个符号(Symbol)被插入到页中,这个符号的引用即变为一个(Function

如是道程序人生 4523

LeetCode(64)分隔链表【链表】【中等】

LeetCode(64)分隔链表【链表】【中等】

ACGkaka的博客 537

【重启后nvidia-smi 命令出现错误】

NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

qq_44769377的博客 5211

Unity开发HoloLens应用:从打包到部署的完整指南

本文为Unity开发者提供了将应用部署到HoloLens设备的完整实战指南。详细讲解了从环境准备、Unity项目配置、生成Visual Studio工程,到最终构建APPX安装包并通过设备门户或Visual Studio进行部署的全流程,帮助开发者高效完成混合现实应用的打包与安装。

red88的博客 1018

86. Partition List

题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。 你应当保留两个分区中每个节点的初始相对位置。 示例: 输入: head = 1-&gt;4-&gt;3-&gt;2-&gt;5-&gt;2, x = 3 输出: 1-&gt;2-&gt;2-&gt;4-&gt;3-&gt;5 方法思路 先来一个错误的代码: 下面的第一块代码是不会通过的...

IPOC_BUPT的博客 240

泛微OA与金蝶云星空深度集成实战:审批与表单双场景解析

本文深入解析泛微OA与金蝶云星空的深度集成实战,聚焦审批流程与表单对接两大核心场景。通过具体案例,详细阐述了如何实现业务数据与审批流程的自动联动,打破数据孤岛,提升企业运营效率与数据一致性,并分享了接口设计、安全策略及实战避坑指南。

4k5l6j7h8的博客 466

LeetCode86 Partition List 分割链表 C++

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...

李歇特冯-兹拜因巴哈的博客 281

erdas监督分类(二)

去云 用一张没有云的影像替代有云影像 构建aoi图层 将云和云影编码合并 重编码工作 在监督分类做的差不多以后(纠正无误,无明显错误),就要对监督分类图像进行重编码和颜色调整。 在中选中Recode,对原来分类的地物类别进行重编码,(为彻底修改,记得备份)选中一列,在New Value输入新的编码,依次编码后点击应用apply,即可实现。 这时的图像如果是全黑的或者只有一两类的颜色也没有关系,新的编码覆盖掉了原来的编码,但是颜色并没有改变才会这样。可以点击图像,右击...

weixin_58968530的博客 958

Qt C++ 卫星数据接收与处理界面

打开 `MainWindow.ui`,你会发现我们完全是在代码中 (`setupUI`) 创建的 UI。* 访问 [QCustomPlot 官网](https://www.qcustomplot.com/) 下载最新版本的 `qcustomplot.h` 和 `qcustomplot.cpp` 文件。* 对于 FFT,可以使用专门的库如 [FFTW](http://www.fftw.org/),或者 Qt 5.15 及以上版本中提供的 `QFFT` 和 `QComplex` 类。

寒寒豆豆软件公司的博客 148

LeetCode 86-Partition List(链表分割)

LeetCode 86-Partition List题解

祝如愿的博客 150

【Bug已解决】codex: conversation context window exceeded / Token limit reached — CodeX CLI 上下文窗口超限解决方案

CodeX CLI 上下文窗口超限(context_length_exceeded)的解决方案,含开新会话、更大窗口模型、精简提示与限制工具输出。

博客 4180

Leetcode 86 Partition List

题意描述 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 ...

herosunly的博客 1万+

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

【小白爬Leetcode86】1.6分隔链表 Partition List

【小白爬Leetcode86】1.6分隔链表 Partition List题目Description中文描述思路一 找到需要插入的节点M,再进行插入思路二 巧用临时头节点 Leetcode 86 medium\color{#FF7D00}{medium}medium 题目 Description Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater

weixin_41604040的博客 185

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【LeetCode】 86. Partition List 分隔链表(Medium)(JAVA)

【LeetCode】 86. Partition List 分隔链表(Medium)(JAVA) 题目地址: https://leetcode.com/problems/partition-list/ 题目描述: Given a linked list and a value x, partition it such that all nodes less than x come before n...

qq_16927853的博客 186

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[LeetCode] 86. Partition List

原题链接:https://leetcode.com/problems/partition-list/ 1. 题目介绍 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 pres...

Ber03的博客 181

Leetcode-86. Partition List

题目描述(中等难度) 感觉中文翻译的比较晦涩,有点不好理解。梳理一下,大致意思是给定一个链表,根据给定的阈值,我们将链表分成两部分,链表左边的部分数字全部小于区分点X,链表右边的部分数字全部大于区分点X。类似快速排序的思想。最终分成两类:122 435. 解法一 主要分为两部分: 1.根据原链表,定义尾指针tail。首先遍历链表,找到第一个大于等于分区点x的节点,我们将tail指向这个节点的...

猫敷雪 457

leetcode探索之旅(132)-86-partition list

今天继续刷leetcode,第86题,将列表分割; 分析: 类似于快排的第一趟,使得base的左边小,右边大,并且要保证左右的区间内元素相对位置不变。 既然是链表,那么可以设置两个指针,一个指针后面接小于base的;一个指针后面接大于等于base的;最后链接两个链表。 问题: 1、指针初始化; 附上c++代码: /** * Definition for singly-linked list. ...

JerryZengZ的博客 212

[LeetCode]86. Partition List

https://leetcode.com/problems/partition-list/ 用两个链表分别保存大于等于和小于的节点,最后将两个链表相连并将large尾节点的下一节点置为空 /** * Definition for singly-linked list. * public class ListNode { * int val; * Li

gqk289的专栏 206

LeetCode 86. Partition List 链表的划分 Python Solution

此题目对应于86. Partition List 题目要求基于一个给定的值x对链表做一个划分partition,使得小于x的放在前,大于等于x的放在后,两部分各自保持原有顺序。 解题思路很简单: 1.设置两个链表,一个存放小于x的元素,一个存放大于等于x的元素 2.合并两个链表,这里需要注意的是需要对第二个辅助链表的表尾进行制空,以防止出现从头到尾的循环链表。 下面附上python代码:

u011452172的博客 535

LeetCode //C - 86. Partition List

【代码】LeetCode //C - 86. Partition List。

Made in Code 832

[leetcode] 86. Partition List

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

TstsUgeg的博客 370

【leetcode】86. Partition List

题目: 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 o...

zxc120389574的博客 121
上一篇: leetcode 283. Move Zeroes
下一篇: leetcode 74. Search a 2D Matrix(二维数组中搜索元素)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值