链表划分-LintCode
来源:互联网 发布:mysql组合主键 编辑:程序博客网 时间:2024/05/18 02:53
给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前。
你应该保留两部分内链表节点原有的相对顺序。
样例:
给定链表 1->4->3->2->5->2->null,并且 x=3
返回 1->2->2->4->3->5->null
#ifndef C96_H#define C96_H#include<iostream>using namespace std;class ListNode{public: int val; ListNode *next; ListNode(int val) { this->val = val; this->next = NULL; }};class Solution {public: /** * @param head: The first node of linked list. * @param x: an integer * @return: a ListNode */ ListNode *partition(ListNode *head, int x) { // write your code here ListNode *p = head, *preHead=new ListNode(0), *postHead=new ListNode(0); ListNode *pre = preHead, *post = postHead; while (p != NULL) { ListNode *next = p->next; if (p->val >= x) { post->next=p; post = post->next; post->next = NULL; } else { pre->next = p; pre = pre->next; pre->next = NULL; } p = next; } pre->next = postHead->next; return preHead->next; }};#endif
阅读全文
0 0
- LintCode-链表划分
- LintCode:链表划分
- Lintcode 链表划分
- LintCode 链表划分
- 链表划分-LintCode
- LintCode : 链表划分
- 链表划分-LintCode
- LintCode 96. 链表划分
- lintcode 96 链表划分
- [LintCode]96.链表划分
- LintCode Partition List 链表划分
- lintcode——链表划分
- [LintCode 96] 链表划分(Python)
- lintcode&九章算法——Lintcode No.96. 链表划分 ? 待解决
- LintCode-数组划分
- LintCode 数组划分
- LintCode:数组划分
- LintCode 数组划分
- JQuery思路
- Servlet入门
- [MySQL] 基础数据类型优化
- Node连接Mongodb以及CRUD操作
- 64位oracle 和64位 PL/SQL 的配置
- 链表划分-LintCode
- KMP与扩展KMP
- 微信小程序之-----简单的navbar
- 布局中的问题
- 秒懂量化选股之最经典的因子选股
- [bzoj1093][ZJOI2007]最大半连通子图 Tarjan,DP
- Mysql学习历程(14)-查询
- LaTeX lstlisting代码块中加入中文注释。
- *树上倍增(LCA)