【Leetcode】【Medium】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.
解题思路:
1、先条件:输入链表不为空;
2、表头可能改变,因此需要新建一个结点指向表头,或使用二维指针;
3、不变参数:
curNode永远指向待操作结点的前驱(方便删减)
small_end永远指向已经分割的所有小结点中,最后一个结点
big_begin永远指向已经分割的所有大结点中,第一个结点
small_end->next = big_begin;
4、curNode->next为NULL时,循环结束。当发现小结点时,插入small_end和big_begin中间;
代码:
/**
* 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 == NULL)
return head;
ListNode* prehead = new ListNode();
prehead->next = head;
ListNode* curNode = prehead;
ListNode* small_end = NULL;
ListNode* big_begin = NULL; while (curNode->next && curNode->next->val < x)
curNode = curNode->next; small_end = curNode;
big_begin = small_end->next; while (curNode->next) {
if (curNode->next->val < x) {
small_end->next = curNode->next;
small_end = small_end->next;
curNode->next = curNode->next->next;
small_end->next = big_begin;
} else {
curNode = curNode->next;
}
} head = prehead->next;
delete prehead;
return head;
}
};
【Leetcode】【Medium】Partition List的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
随机推荐
- stm32CubeMx 实现单通道ADC DMA采集
今天要做的是ADC单通道DMA采集实验 MCU : STM32F429 开发工具:STM32CubeMx 版本号 5.0.0 实验目的:实现ADC1 13通道 DMA采集 一 :简介 首先,我们来看一 ...
- Failed to start NodeManager caused by "/var/lib/hadoop-yarn/yarn-nm-recovery/yarn-nm-state/LOCK: Permission denied"
Hadoop 安装步骤: 0. 安装前准备(节点机器,环境设置,yum源设置) 1. 配置并安装Cloudera-Manager 2. 启动 CM 服务 3. 安装CDH,并配置集群 4. 启动 ...
- Delphi设置表格样式
//设置表格样式wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).LineStyle = Word.WdLineS ...
- <a>标签里面嵌图片<img>下面出现一小段空白的原因
今天做项目的时候,发现在a标签,里面嵌入<img>会出现空白 css 内容: a{ border:1px solid black; } img{ width:200px; } html内容 ...
- WPF的布局--DockPanel
1.DockPanel: 以上.下.左.右.中为基本结构的布局方式 类似于Java AWT布局中的BorderLayout. 但与BorderLayout不同的是,每一个区域可以同时放置多个控件,在同 ...
- linux下MySQL停止和重启
一.启动方式1.使用linux命令service 启动:service mysqld start2.使用 mysqld 脚本启动:/etc/inint.d/mysqld start3.使用 safe_ ...
- 完整maven项目搭建
1. 在eclipse中New选择maven项目,并选择web类型这个百度一下到处有: 2. 写简单的controller示例: 3. 配置web.xml,主要是DispatcherServlet: ...
- centos 中输入ifconfig 只有lo 没有eth0
问题描述:linux中输入ifconfig命令,只有lo,没有eth0 解决方法: 1.进入/etc/sysconfig/network-scripts 目录,发现有ifcfg-eth0,即网卡(驱动 ...
- 第七章使用java实现面向对象- 多线程
一.Thread类和Runnable接口 1.在java.lang包中定义了Runnable接口和Thread类. Runnable接口中只定义了一个方法,它的格式为: public abstract ...
- svn在commit后报错:is scheduled for addition, but is missing
今天通过svn 的cr(code review)代码审核后,我欲执行svn ci -m"xxxxxxx(提交注释) ISSUE=3380305",但是没有提交成功,SVN报错啦! ...