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 ...
随机推荐
- Android Studio Debug
小米4usb调试怎么打开?miui6进入开发者模式想要打开USB调试首先开启开发者模式.过去在MIUI V5版本时,小米手机开启开发者模式的方法是连续点击Anroid版本号.不过最新上市的小米4都搭载 ...
- Debian 7设备nginx周围环境、编译并安装和启动
/********************************************************************* * Author : Samson * Date ...
- 应用spss可靠性分析软件
问卷调查的可靠性分析 一.概念: 信度是指依据測验工具所得到的结果的一致性或稳定性,反映被測特征真实程度的指标. 一般而言,两次或两个測验的结果愈是一致.则误差愈小,所得的信度愈高,它具有下面 ...
- Objective-C基调(4)Category
OC它提供了一种不同的方式--Category,可以动态地添加新的行为已经存在的类(方法),这确保了较小的类的原始设计,然后逐渐加入扩展. 正在使用Category扩张的上课时间,你并不需要创建一个子 ...
- Testin一日游实验室发布的行级APP质量报告:在那里拍携程双赢
Testin实验室公布国庆出行旅途类APP质量报告:携程力压去哪儿夺冠 2014/09/28 · Testin · 实验室报告 一年一度的十一黄金周即将临近,旅游软件成为每外出行人手机必装软件.为此全 ...
- Python输出文件由线解释和扩展的具体内容
结束此处的测试代码中作者写的,第一段包含不正确的版本号,后者是正确的版本号: #! /usr/bin/python2.7 try: filename = raw_input('please i ...
- Session or Cookie?是否有必要使用Tomcat等一下Web集装箱Session
Cookie是HTTP协议标准下的存储用户信息的工具.浏览器把用户信息存放到本地的文本文件里. Session是基于Cookie实现的. 2011年4月,武汉群硕面试的时候(实习生).面试官也问过这个 ...
- freemarker定义自己的标签错误(一)
freemarker定义自己的标记 1.错误描写叙述 freemarker.core.ParseException: Token manager error: freemarker.core.Toke ...
- javascript系列之DOM(一)
原文:javascript系列之DOM(一) DOM(document object moudle),文档对象模型.它是一个中立于语言的应用程序接口(API),允许程序访问并修改文档的结构,内容和样式 ...
- hdoj 1226 超级password 【隐图BFS】
称号:hdoj 1226 超级password 分析:这题属于隐式图搜索,状态不是非常明显,须要自己建立. 事实上搜索说白了就是暴力. 这个题目就是,首先对给出的能够组成的全部的数依次枚举.长度从小到 ...