LeetCode(86) Partition List
题目
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.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
分析
给定一个链表与一个目标值x,要求对链表结点按照以下要求重排:
(1)值小于x的结点,按照其原顺序排列与链表头部
(3)其余值不小于x的结点,按照其原顺序链接与尾部
AC代码
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if (!head || !head->next)
return head;
ListNode *p = head;
head = NULL;
ListNode *r = head;
//(1)寻找第一个不小于目标值的节点p,前n个小于x的节点逐个连接到head,保存尾结点r
while (p && p->val < x)
{
if (!head)
{
head = p;
r = head;
}
else{
r->next = p;
//保存新链表尾结点
r = r->next;
}
p = p->next;
r->next = NULL;
}//while
//如果此时p为空,已经没有其余节点,返回新链表head
if (!p)
return head;
//(2)p节点大于x,从下一个结点开始遍历,将小于x的结点连接到head中,原链表删除该结点
ListNode *pre = p , *q = p->next;
while (q)
{
if (q->val < x)
{
if (!head)
{
head = q;
r = head;
}
else{
r->next = q;
//保存新链表尾结点
r = r->next;
}
pre->next = q->next;
q = q->next;
r->next = NULL;
}//if
else{
pre = q;
q = q->next;
}//else
}//while
//如果此时,原链表还剩余结点,直接连接到r后面即可
if (p)
{
if (!head)
return p;
else
r->next = p;
}
return head;
}
};
LeetCode(86) Partition List的更多相关文章
- LeetCode(86):分隔链表
Medium! 题目描述: 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: hea ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- 单表:1.查询全部 2.条件查询 JSP Servlet
- 题解报告:hdu 1969 Pie(二分)
Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no ...
- neo4j(图数据库)是什么?
不多说,直接上干货! 作为一款强健的,可伸缩的高性能数据库,Neo4j最适合完整的企业部署或者用于一个轻量级项目中完整服务器的一个子集存在. 它包括如下几个显著特点: 完整的ACID支持 高可用性 轻 ...
- Suricata的总体架构
Suricata的总体架构 报文检测系统通常四大部分,报文获取.报文解码.报文检测.日志记录:suricata不同的功能安装模块划分,一个模块的输出是另一个模块的输入,suricata通过线程将模块 ...
- php多文件上传类(含示例)
在网上看到一个比较好的多文件上传类,自己改良了下,顺便用js实现了多文件浏览,php文件上传原理都是相同的,多文件上传也只是进行了循环上传而已,当然你也可以使用swfupload进行多文件上传! &l ...
- windows 7 正确禁用 IPv6
与Windows XP和Windows Server 2003不同的是,Windows Vista和Windows Server 2008中的IPv6无法被卸载.然而,在Windows Vista和W ...
- JavaScript禁止键入非法值,只有这些才能被键入
JavaScript禁止键入非法值,只有这些才能被键入(k==9)||(k==13)||(k==46)||(k==8)||(k==189)||(k==109)||(k==190)||(k==110)| ...
- 代码文件导到word里
# 创建并写入word文档 import docx import sys import os dir = "D:\\gitwork\\fm.qimeng.c-class\\src\\main ...
- 架包Error inflating class错误
当引用架包后,出现Error inflating class错误时通常要检测架包是否正确引用: 1.首先将你所需要的架包拷贝到工程目录下: 2.右击工程,选择Build Path-->confi ...
- sqlserver:查询锁住sql以及解锁
--查看被锁表:SELECT request_session_id spid, OBJECT_NAME( resource_associated_entity_id ) tableNameFROM s ...