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 ...
随机推荐
- Eclipse新建Maven webapp项目错误的解决方法
新建webapp项目时出现如下错误: 解决步骤如下: 1. 右键点击项目,选择Properties,点击Java Build Path,将默认的JRE移除,点击右侧add Library,选择JRE ...
- java实现数据结构
数据结构与算法 :一.数据结构和算法简介 数据结构是指数据在计算机存储空间中的安排方式,而算法时值软件程序用来操作这些结构中的数据的过程.二. 数据结构和算法的重要性 几乎所有的程序都会使用到数据结构 ...
- Hue的全局配置文件hue.ini(图文详解)
Hue版本:hue-3.9.0-cdh5.5.4 需要编译才能使用(联网) 说给大家的话:大家电脑的配置好的话,一定要安装cloudera manager.毕竟是一家人的.同时,我也亲身经历过,会有部 ...
- hdu 1558 Segment set 计算几何+并查集★
#include <cstdio> #include <iostream> #include <string.h> using namespace std; ; # ...
- python实现基数排序
# 基数排序有着局限性,只能是整数,# 排序的时候要先排后面一个条件的(多条件排序)#如本例中,先从个位开始排起# 多关键字排序# 从低关键字开始排序 # @File: radix_sort #### ...
- udp聊天交互
#****server端 import socket sk = socket.socket(type = socket.SOCK_DGRAM) sk.bind(('127.0.0.1', 8050)) ...
- Traffic Real Time Query System HDU - 3686
https://vjudge.net/problem/HDU-3686 点双啊,就是在求割顶的时候,另外用一个栈来存一些边 在遍历u点出发的边时,遇到树边或反向边(u,v)就把此边加入栈(可能要记一下 ...
- ES相关概念理解
Elasticsearch特点:分布式,高性能,高可用,高伸缩的搜索和分析: 1)可作为一个大型分布式集群,处理PB级别的数据,服务大型公司,亦可运行在少数或单台设备上服务小型公司 分布式的特性: E ...
- [转]VC中调用外部exe程序方式
本文转自:http://blog.sina.com.cn/s/blog_486285690100ljwu.html 目前知道三种方式:WinExec,ShellExecute ,CreateProce ...
- poj2139 Six Degrees of Cowvin Bacon
思路: floyd 实现: #include <iostream> #include <cstdio> #include <cstring> #include &l ...