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. POST请求出现中文乱码的问题

    最近使用Java的HttpURLConnection请求rest接口时候,POST请求参数中的中文传输之后出现乱码的问题,在网上找了一个亲测有效的方法: 将 DataOutputStream out ...

  2. CentOS6.5安装testlink1.9.14

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

  3. Rational Rose2007下载和安装

    网上关于Rational Rose2007安装包,网上找了一堆大多都是垃圾,最后找到一个可用的(带激活文件),保存在自己的网盘里,这里分享出来:https://pan.baidu.com/s/1bpb ...

  4. Ldap用户登陆操作系统提示Permission denied, please try again.

    昨天一个同事he告诉我他的ldap账户无法登录系统,提示Permission denied, please try again 解决方法: 先在百度上找了下别人的博客参考了下 https://blog ...

  5. restful api理解

    REST -- REpresentational State Transfer 直接翻译:表现层状态转移. 首先要明确一点:REST 实际上只是一种设计风格,它并不是标准. 0. REST不是&quo ...

  6. 深入理解JavaScript系列(47):对象创建模式(上篇)

    介绍 本篇主要是介绍创建对象方面的模式,利用各种技巧可以极大地避免了错误或者可以编写出非常精简的代码. 模式1:命名空间(namespace) 命名空间可以减少全局命名所需的数量,避免命名冲突或过度. ...

  7. [转]ORA-00979: not a GROUP BY expression报错处理

    本文转自:http://blog.itpub.net/29154652/viewspace-772504/ 环境:Oracle Database 11gR2(11.2.0.2) on Linux  故 ...

  8. bitbucket 源代码托管

    5个人以下可以免费使用,不限制仓库的数量; 国外的注册需要开启蓝灯FQ; 1.注册账号 maanshancss w1-g1@qq.com;创建仓库; 然后拷贝现有项目 然后提交 然后push; 2.写 ...

  9. redis(7)LRU缓存

    一.LRU简介 LRU是Least Recently Used的缩写,即:最近最少使用. 它是内存管理中的一种页面置换算法,对于在内存中但是又不用的数据块,操作系统会根据哪些数据属于LRU而将其移除内 ...

  10. SQL语句整理(二) 数据定义语言DDL

    前言: 这是我学数据库时整理的学习资料,基本上包括了所以的SQL语句的知识点. 我的教材是人大王珊老师的<数据库系统概论>. 因为是手打的,所以会用一些细节打错了,但都挺明显也不多(考完试 ...