题目链接:https://leetcode.com/problems/partition-list/description/

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(>= x)的结点拎出来,也可将小于x(< x)的结点拎出来,两者处理过程基本一样。只是组合成所求链表时插入位置不同。

编码如下

 /**
* 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 == nullptr) return head; // 链表为空,则直接返回 // 操作原始链表的准备工作
// 包括创建一个辅助结点指向链表的头结点
ListNode *pHead = new ListNode(-);
pHead->next = head;
ListNode *pre = pHead; // pre指向遍历到的结点的前驱结点
ListNode *cur = head; // cur指向遍历到的当前结点(即待处理结点) // 创建指向大于等于x(>= x ) 结点的链表
// 用pNew指针指向该链表的头结点,pNew为辅助结点
ListNode *pNew = new ListNode(-);
ListNode *ptrNew = pNew; // 对原始链表进行遍历
while (cur != nullptr)
{
if (cur->val >= x) // 将原始链表中大于等于x(>= x)的结点分离出来,形成新的链表
{
ListNode *pTemp = cur;
pre->next = pTemp->next;
cur = cur->next; ptrNew->next = pTemp;
ptrNew = ptrNew->next;
ptrNew->next = nullptr;
}
else // 对于原始链表中小于x(< x)的结点,移动指针指向即可。
{
pre = cur;
cur = cur->next;
}
} // 将两部分连接起来
// 将原始链表中小于x(< x)的部分和原始链表中大于等于x(>= x)的部分连接起来
pre->next = pNew->next;
head = pHead->next; // 释放辅助结点内存空间
delete pHead;
delete pNew; return head;
}
};

086. Partition List的更多相关文章

  1. [LeetCode]题解(python):086 - Partition List

    题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...

  2. 086 Partition List 分隔链表

    给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前.你应当保留两个分区中每个节点的初始相对位置.例如,给定1->4->3->2-&g ...

  3. Partition:增加分区

    在关系型 DB中,分区表经常使用DateKey(int 数据类型)作为Partition Column,每个月的数据填充到同一个Partition中,由于在Fore-End呈现的报表大多数是基于Mon ...

  4. Partition:Partiton Scheme是否指定Next Used?

    在SQL Server中,为Partition Scheme多次指定Next Used,不会出错,最后一次指定的FileGroup是Partition Scheme的Next Used,建议,在执行P ...

  5. Partition:分区切换(Switch)

    在SQL Server中,对超级大表做数据归档,使用select和delete命令是十分耗费CPU时间和Disk空间的,SQL Server必须记录相应数量的事务日志,而使用switch操作归档分区表 ...

  6. sql 分组取最新的数据sqlserver巧用row_number和partition by分组取top数据

    SQL Server 2005后之后,引入了row_number()函数,row_number()函数的分组排序功能使这种操作变得非常简单 分组取TOP数据是T-SQL中的常用查询, 如学生信息管理系 ...

  7. Oracle Partition Outer Join 稠化报表

    partition outer join实现将稀疏数据转为稠密数据,举例: with t as (select deptno, job, sum(sal) sum_sal from emp group ...

  8. SQLServer中Partition By 函数的使用

    今天群里看到一个问题,在这里概述下:查询出不同分类下的最新记录.一看这不是很简单的么,要分类那就用Group By;要最新记录就用Order By呗.然后在自己的表中试着做出来: 首先呢我把表中的数据 ...

  9. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

随机推荐

  1. 把所有时间用来做你最应该做的事,用尽全力竭尽所能成为DL and NLP大神。

    两段代码,JAVA and CPP,输出相同结果: #include "stdafx.h" #include <iostream> using namespace st ...

  2. java中用activiti插件连接mysql数据库,自动建表过程中,在配置mysql架包路径“org.activiti.engine.ActivitiException: couldn't check if tables “

    java中用activiti插件连接mysql数据库,出现错误: org.activiti.engine.ActivitiException: couldn't check if tables are ...

  3. 【NOIP2012】同余方程

    原题: 求关于xx的同余方程ax≡1(mod b)的最小正整数解. 裸题 当年被这题劝退,现在老子终于学会exgcd了哈哈哈哈哈哈哈哈 ax≡1(mod b) => ax=1+by => ...

  4. Python模拟浏览器多窗口切换

    # 模拟浏览器多窗口切换 # 代码中引入selenium版本为:3.4.3 # 通过Chrom浏览器访问发起请求 # Chrom版本:59 ,chromdriver:2.3 # 需要对应版本的Chro ...

  5. 【WinForm-TreeView】实现Win7 Areo效果

    效果图: 新建一个继承自TreeView的控件类,代码如下: using System; using System.Windows.Forms; using System.Drawing; using ...

  6. 【leetcode】1286. Iterator for Combination

    题目如下: Design an Iterator class, which has: A constructor that takes a string characters of sorted di ...

  7. AcWing:111. 畜栏预定(贪心 + 小根堆)

    有N头牛在畜栏中吃草. 每个畜栏在同一时间段只能提供给一头牛吃草,所以可能会需要多个畜栏. 给定N头牛和每头牛开始吃草的时间A以及结束吃草的时间B,每头牛在[A,B]这一时间段内都会一直吃草. 当两头 ...

  8. ARTS打卡计划第二周

    Algorithms: https://leetcode-cn.com/problems/3sum/ 算法是先排序,然后按照两个数和两边逼中,考虑去重. Review: https://www.inf ...

  9. java基础阶段几个必会面试题

    摘自:https://www.cnblogs.com/zn19961006/p/11869182.html java基础阶段几个必会面试题 目录 1.说出你对面向对象的理解 在我理解,面向对象是向现实 ...

  10. TCP输入 之 tcp_rcv_established

    概述 tcp_rcv_established用于处理已连接状态下的输入,处理过程根据首部预测字段分为快速路径和慢速路径: 1. 在快路中,对是有有数据负荷进行不同处理: (1) 若无数据,则处理输入a ...