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. npm 包管理工具

    能注册后看简单的功能 订单加信息 下单之前的判断要配合海潮的迁移数据 运行自定义的脚本 在 package.json 的 scripts 里添加自定义的结点 ( 比如 CSOR-serve ) &qu ...

  2. sizeof(数组名) 与 数组长度

    int a[] = {1, 2, 3, 4}; cout << sizeof(a); //16 char b[] = "abc"; cout << size ...

  3. ubuntu 16.04 tensorboard 学习

    一.新建tensorboard的文件夹,并在该文件夹下打开终端进入python输入以下代码 ////////新建文件夹取名tensorboard 在该目录下打开终端 import tensorflow ...

  4. opencv-python 读入带有中文的图片路径

    windows 下读入带有中文的图片路径使用cv2.imread() 不能读入.使用如下代码可以. def cv_imread(filePath): cv_img = cv2.imdecode(np. ...

  5. (转)haproxy启动故障:Starting proxy:cannot bind socke

    haproxy启动时提示失败: [ALERT] 164/110030 (11606) : Starting proxy linuxyw.com: cannot bind socket 这个问题,其实就 ...

  6. EntityFrameWork Code First 一对多关系处理

    场景1: 一个文章类别(Category)下含有多篇文章(Article),而某篇文章只能对应一个类别 Article和Category的代码如下: /// <summary> /// 文 ...

  7. html中使用滚动条

    1. 在html页面中使用滚动条,效果如下: 代码如下: <div style="height:auto !important;max-height:200px;overflow:sc ...

  8. React.js 小书 Lesson21 - ref 和 React.js 中的 DOM 操作

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson21 转载请注明出处,保留原文链接和作者信息. 在 React.js 当中你基本不需要和 DO ...

  9. sublime text 3 3143注册码

    1.点击help->enter license: —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390 ...

  10. Expression Blend实例中文教程(11) - 视觉管理器快速入门Visual State Manager(VSM)

    Visual State Manager,中文又称视觉状态管理器(简称为VSM),是Silverlight 2中引进的一个概念.通过使用VSM,开发人员和设计人员可以轻松的改变项目控件的视觉效果,在项 ...