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的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【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 ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【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 ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. 【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 ...

  10. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

随机推荐

  1. GPRS的短信和打电话功能

    短信功能: 发短信设置文本格式就可以了:但收短信可能收到的是乱码,需要编写解码程序才可以: 关于打电话单片机复位功能: 首先要建立黑白名单制度过滤手机号,只运行白名单的手机对的单片机打电话:其它的不响 ...

  2. Java框架-mybatis03使用注解实现mybatis

    1.面向接口编程: 好处:扩展性好,分层开发中,上层不用管具体的实现,都遵循共同的标准,使得开发变得容易.规范性更好 2.注解的实现 a)编写Dao接口 public interface UserDa ...

  3. CentOS6.5安装testlink1.9.14

    前提条件:准备一台CentOS6.5虚拟机,配置好IP,关闭iptables和selinux. 这里提供上我的云盘软件,可以去这里下载:http://pan.baidu.com/s/1qXymele ...

  4. 浅谈Cordova优缺点与环境部署(转载)

    浅谈Cordova优缺点与环境部署 作者:苏华杰 简介 Cordova是一个用基于HTML.CSS和JavaScript的,用于创建跨平台移动应用程序的快速开发平台.它使开发者能够利用iPhone.A ...

  5. 转 关于shell中if 语法结构的广泛误解

    转自 ttp://blog.csdn.net/security134/article/details/6742156 最近学习SHELL编程 这篇文章很好很重要.有些东西不能想当然.同时不是表面看起来 ...

  6. 浏览器对document.all的支持差异

    从何而来从IE4开始IE的object model才增加了document.all对象,MSDN中也对 Object.all 有详细的说明,Object.all是个HTMLCollection,不是数 ...

  7. 【c++】iostreeam中的类为何不可以直接定义一个无参对象呢

    缘起 #include <iostream> #include <fstream> #include <sstream> using namespace std; ...

  8. java获取request的头信息

    1.获取全部头信息: //get request headers private Map<String, String> getHeadersInfo() { Map<String, ...

  9. log4j.properties 基本配置

    log4j.rootLogger=WARN,stdout,D log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender ...

  10. Magento 2开发教程 - 创建新模块

    视频在youtube网站国内访问不了,可以使用FQ软件查看. 视频地址:www.youtube.com/embed/682p52tFcmY@autoplay=1 下面是视频文字介绍: Magento ...