题目链接: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. 8080 端口被占用的解决方法 netstat -ano;taskkill (命令行)

    8080 端口被占用的解决方法 netstat -ano:taskkill (命令行) (ano 和 aon 都可以) 打开命令行: (1)netstat -ano 可查看端口使用情况,记住 PID ...

  2. cmd_memo

    1. bind host or ip:port #指定域名 curl -H 'Host:www.tsuiz.com' http://10.14.54.131:8080/check.do #指定ip和端 ...

  3. Servlet中的请求转发RequestDispatcher接口的forword与Include的区别

    RequestDispatcher接口中具有两个方法: forward() 与 include() 均 可完成请求 的转发.区别如下: forword(): 使用该方法,则当前 的 Servlet 中 ...

  4. css3常用动画大全:translate、scale、opacity、rotate (转)

    /* animation */ .a-bounce,.a-flip,.a-flash,.a-shake,.a-swing,.a-wobble,.a-ring{-webkit-animation:1s ...

  5. 51 Nod 1629 B君的圆锥

    1629 B君的圆锥  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 B君要用一个表面积为S的圆锥将白山云包起来. B君希望包住的白山云体积尽量大 ...

  6. http状态码301和302详解及区别——辛酸的探索之路

    原文链接:https://blog.csdn.net/grandPang/article/details/47448395 一直对http状态码301和302的理解比较模糊,在遇到实际的问题和翻阅各种 ...

  7. A. Odds and Ends(思维)

    A. Odds and Ends time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  8. npm+cnpm+vuecli3打包相关

    1,npm install和cnpm install时的不同 https://blog.csdn.net/csm0912/article/details/90264026 2,npm设置和查看仓库源 ...

  9. Hbase Bulkload

    前言 Apache HBase 是目前大数据系统中应用最为广泛的分布式数据库之一.我们经常面临向 HBase 中导入大量数据的情景,通常会选择使用标准的客户端 API 对 HBase 进行直接的操作, ...

  10. cocos2dx热更新之后,闪退问题记录。

    如果使用cocos2dx的3.17.2版本的官方热更新. 然后有玩家反馈说热更新之后游戏闪退,游戏内有部分资源没更到. 考虑如下几个方面调整. 1,在文件下载失败的时候,直接调用重新下载. 2,把下载 ...