leetcode[87] Partition List
题目:给定一个链表和一个数x,将链表中比x小的放在前面,其他的放在后头。例如:
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
思路:
1. 再用两个node,一个指向所有小于x的,一个指向其他的,之后把两个接在一起。接在一起需要注意large是否未移动过。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *partition(ListNode *head, int x)
{
if (head == NULL || head -> next == NULL) return head;
ListNode *ans_small = new ListNode(); // 存小于x的
ListNode *ans_large = new ListNode(); // 存不小于x的
ans_small -> next = head;
ans_large -> next = head;
ListNode *small = ans_small;
ListNode *large = ans_large;
ListNode *cur = head; while(cur)
{
if (cur -> val < x)
{
small -> next = cur;
small = cur;
cur = cur -> next;
}
else
{
large -> next = cur;
large = cur;
cur = cur -> next;
}
}
large -> next = NULL; // 这个是为了防止large指向head的时候
small -> next = ans_large -> next;
head = ans_small;
delete ans_small, ans_large;
return head -> next;
}
};
2. 就创建一个node,这个node遇见比x小的就插入
class Solution {
public:
ListNode *partition(ListNode *head, int x)
{
if (head == NULL || head -> next == NULL) return head;
ListNode *ans = new ListNode();
ans -> next = head;
ListNode *small = ans; // 在它的后面插入小的
ListNode *cur = head;
ListNode *pre = ans; // cur的前一个指针,方便插入操作
bool flag = true; // true说明要插入的紧接着就是下一个,那就不用插入,加加就好
while(cur != NULL)
{
if (cur -> val < x && small -> next == cur) // 待插入的和之前小的相邻就不用插入了
{
pre = pre -> next;
cur = cur -> next;
small = small -> next;
}
else if (cur -> val < x && small -> next != cur) // 不相邻,插入
{
ListNode *tmpnext = small -> next;
small -> next = cur;
small = cur;
cur = cur -> next;
small -> next = tmpnext;
pre -> next = cur;
flag = true;
}
else if (cur -> val >= x)
{
flag = false;
cur = cur -> next;
pre = pre -> next;
}
}
head = ans;
delete ans;
return head -> next;
}
};
从第二个思路中知道了原来可以判断连个node是否相等!这个之前以为不能那样判断的,原来可以用 if(node1 == node2)。多学一个知识点了。
leetcode[87] Partition List的更多相关文章
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- LeetCode 87,远看是字符串其实是搜索,你能做出来吗?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第54篇文章,我们一起来看LeetCode 87题,Scramble String(爬行字符串). 这题的官方难度 ...
- [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- [LeetCode] 763. Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] 87. Scramble String 搅乱字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- LeetCode 1043. Partition Array for Maximum Sum
原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...
- [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 ...
随机推荐
- 初识 Cloudera Impala
Impala是Cloudera公司主导开发的新型查询系统,它提供SQL语义,能查询存储在Hadoop的HDFS和HBase中的PB级大数据.已有的Hive系统尽管也提供了SQL语义,但因为Hive底层 ...
- BI—脚不一样的感觉
在这个网络智能的时代,假设生活和智能挂不上边那就太落后啦!尤其IT行业更是如此,前不久还在用微软的office做报表,这几天就吵吵着换成BI,那么BI是什么?有什么用?怎么用?等等带着这一系列的问题来 ...
- (大数据工程师学习路径)第五步 MySQL参考手册中文版----MySQL数据库管理
一.MySQL权限管理 账户权限信息被存储在mysql数据库的user.db.host.tables_priv.columns_priv和procs_priv表中. GRANT和REVOKE语句所用的 ...
- SQLServer-----SQLServer 2008 R2卸载
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVrZXdhbmd6aQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 【日报C在23】堆和栈的深入了解
每日一C之堆与栈的深入理解 每天拾一个C语言贝壳,厚积薄发,积跬步以致千里. 今日贝壳:内存中堆与栈的深入理解.认识一个清晰地内存 假 ...
- 快速解读GC日志(转)
本文是 Plumbr 发行的 Java垃圾收集手册 的部分内容.文中将介绍GC日志的输出格式, 以及如何解读GC日志, 从中提取有用的信息.我们通过 -XX:+UseSerialGC 选项,指定JVM ...
- 应用程序框架实战十三:DDD分层架构之我见(转)
前面介绍了应用程序框架的一个重要组成部分——公共操作类,并提供了一个数据类型转换公共操作类作为示例进行演示.下面准备介绍应用程序框架的另一个重要组成部分,即体系架构支持.你不一定要使用DDD这样的架构 ...
- Nyoj 网络的可靠性(水题)
描述 A公司是全球依靠的互联网解决方案提供商,也是2010年世博会的高级赞助商.它将提供先进的网络协作技术,展示其”智能+互联“的生活概念,同时为参观者提供高品质的个人体验和互动,以”信息通信,尽情城 ...
- RabbitHub开源
RabbitHub开源情况及计划 之前写过一篇”.NET 平台下的插件化开发内核(Rabbit Kernel)”,已经过去三个月了,期间RabbitHub并不是没有了发展更不是放弃了发展,在Rab ...
- background-position 具体的使用说明
语法: background-position : length || length background-position : position || position 值: length : ...