LeetCode 86. 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 two partitions.
Example:
Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5
有点用到了倒置链表II的方法,将符合要求的结点放置在指向pre指针的后面。这道题的思路应该是找到第一个大于等于x值的结点,他前一个位置始终定位pre指针,放置比x小的结点。
方法一(C++)
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* dummy=new ListNode(-);
dummy->next=head;
ListNode* pre=dummy,* cur=head;
while(pre->next&&pre->next->val<x)
pre=pre->next;
cur=pre;
while(cur->next){
if(cur->next->val<x){
ListNode* t=cur->next;
cur->next=t->next;
t->next=pre->next;
pre->next=t;
pre=t;
}
else
cur=cur->next;
}
return dummy->next;
}
};
LeetCode 86. Partition List 划分链表 C++的更多相关文章
- [LeetCode] 86. Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [leetcode]86. Partition List划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [LeetCode]86. Partition List分离链表
/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...
- leetCode 86.Partition List(分区链表) 解题思路和方法
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [CareerCup] 2.4 Partition List 划分链表
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- leetcode 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- partition List(划分链表)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [LeetCode] 86. Partition List 解题思路
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- mysql 主主+主从笔记
环境 Ubuntu 14.04.4 LTS *3 分别是master1(192.168.42.28), master2(192.168.42.29), slave1(192.168.42.33)测试下 ...
- wps excel
ET.Application etApp;ET.workbook etbook;ET.Worksheet etsheet ;ET.Range etrange;//获取工作表表格etApp = new ...
- java的接口
接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并不是类,编写接口 ...
- 查看python的OpenCV版本
安装"opencv" pip install opencv-python查看版本 import cv2 cv2.__version__
- 跳台阶(JAVA)
跳台阶 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 思路:典型的动态规划问题,动态规划问题最关键的是把事件中的各种情形抽象为状态,然后找到前后 ...
- expdp之include参数——实现表级别的expdp操作
需求是这样的:想将A库的某schema中的一部分表导入到B库的某schema中. 第一可以想到的是使用expdp工具,但是如何只挑选某些表呢,通过查看官方文档,include参数可以实现该需求. in ...
- LVS详细介绍以及遇到的坑
LVS详细介绍以及遇到的坑 一,概述 本文介绍了我搭建LVS集群的步骤,并且在使用LVS(Linux Virtual Server)过程中遇到的问题和坑, 二,LVS简单介绍 大家都知道,LVS中文意 ...
- python 将本地目录暴露为http服务
python3 nohup python3 -m http.server 8080 &
- Python学习:列表、元组、字典、集合
转载:https://www.cnblogs.com/xc-718/p/9632942.html 列表/元组 列表和元组都是序列结构,它们本身很相似,但又有一点不同: 列表是用方括号标记,如:a=[1 ...
- error: Cannot find OpenSSL's <evp.h> Mac
问题 mac安装php需要openssl ./configure –with-openssl 报错 error: Cannot find OpenSSL’s 解决 brew install opens ...