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 ...
随机推荐
- [SignalR]初步认识以及安装
原文:[SignalR]初步认识以及安装 1.什么是ASP.NET SignalR? ASP .NET SignalR是一个 ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时 ...
- NSIS:使用nsWindows.nsh头文件调整窗体大小
原文 NSIS:使用nsWindows.nsh头文件调整窗体大小 此方法只能简单实现调整窗体大小,但不完美,调整后窗体上其他的控件都需要调整大小或位置,比较麻烦,轻狂不建议使用呵! ;加头文件!inc ...
- SVD在推荐系统中的应用
一.奇异值分解SVD 1.SVD原理 SVD将矩阵分为三个矩阵的乘积,公式: 中间矩阵∑为对角阵,对角元素值为Data矩阵特征值λi,且已经从大到小排序,即使去掉特征值小的那些特征,依然可以很好地重构 ...
- hdoj 1506&&1505(City Game) dp
// l表示从l[i]到i连续大于a[i]的最远左区间.r表示从i到r[i]连续大于a[i]的最远又区间 DP 找出 a[i] 的最远左区间和最远右区间与自己连着的比自己大的数的长度 , 然后用这个长 ...
- JSF+EJB+JPA总体思路
前言: JSF+EJB+JPA 其实我并没有想象中的难,只是想做好,建立在正确的地方应用,真正的困难. 良好的技术,在错误的地方做应用,这是唯一能够被垃圾. 用. 重量级企业应用能够使用这个主要的3层 ...
- JSP简单的练习-使用JDOM阅读xml文件
<%@ page contentType="text/html; charset=gb2312" language="java" %> <%@ ...
- js中document.write()使用方法
<script> var hrf = window.location.href; if(hrf.indexOf('change')>0){ ...
- Leetcode dfs Combination SumII
Combination Sum II Total Accepted: 13710 Total Submissions: 55908My Submissions Given a collection o ...
- 使用php-fpm状态页观察当前的php-fpm状态
对于php-fpm的參数设置,非常多情况下有这种疑问,就是内置的几个參数比如pm.max_children,pm.start_servers等这几个參数究竟该设置最多为多少才合适.事实上这几个參数往往 ...
- javascript实现数据结构:广义表
原文:javascript实现数据结构:广义表 广义表是线性表的推广.广泛用于人工智能的表处理语言Lisp,把广义表作为基本的数据结构. 广义表一般记作: LS = (a1, a2, ..., an ...