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 ...
随机推荐
- windows下配置java环境jdk
Windows系统下搭建java的开发环境和配置环境变量 具体步骤打开链接地址:https://www.cnblogs.com/lijuntao/p/6694483.html
- matplotlib 绘图实例01:正弦余弦曲线
该讲的实例结果如下图所示: 第01步:导入模块,并设置显示中文和负号的属性: import matplotlib.pyplot as plt import numpy as np plt.rcPara ...
- HAL之EXIT
在STM32cubeMX中 1 在GPIO管脚上选定EXIT功能 2 在GPIO模式中设定触发边沿类型 3 在NVIC中设定NVIC分组及使能EIXT_Line0_interrupt 在MDK中的GP ...
- codeforces415D. Glad to see you!(交互)
题意 交互题. 有$k$个值域为$[1, n]$的数. 请在不超过$60$次询问内找出其中的两个数. 每次询问形式为1 x y 交互库会返回$|x - a| <= |y - b| ? " ...
- nodejs中相互引用(循环引用)的模块分析
话不多少,直接上源码吧: modA.js: module.exports.test = 'A'; const modB = require('./05_modB'); console.log( 'mo ...
- coursera网站中的VTT字幕的使用
coursera网站中的VTT字幕的使用 1.https://www.coursera.org/learn/os-virtsecurity/lecture/xuWgP/1-3-cao-zuo-xi-t ...
- android studio 定时器操作 实现定时执行相关任务
package ipget.wenzheng.studio.ipget; import android.os.Bundle; import android.os.Handler; import and ...
- 原生JS--各种兼容性写法总结
<script> var oEvent = evt || event; ========================================================== ...
- 朴素贝叶斯法(naive Bayes)
<统计学习方法>(第二版)第4章 4 朴素贝叶斯法 生成模型 4.1 学习与分类 基于特征条件独立假设学习输入输出的联合概率分布 基于联合概率分布,利用贝叶斯定理求出后验概率最大的输出 条 ...
- xls表格 拼接字段 拼json =CONCAT("{ code:'",A2,"',","codeName: '",B2,"',","flag: '",C2,"'},")
xls表格 拼接字段 拼json =CONCAT("{ code:'",A2,"',","codeName: '",B2,"',& ...