Leetcode 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
.
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(head == NULL || head->next == NULL) return head;
ListNode* p = new ListNode(), *pre_p = p;
ListNode *q = new ListNode(), *pre_q = q;
while(head){
if(head->val < x){
ListNode *tmp = head->next;
head->next = pre_p->next;
pre_p->next = head;
pre_p = pre_p->next;
head=tmp;
}else{
ListNode *tmp = head->next;
head->next = pre_q->next;
pre_q->next = head;
pre_q = pre_q->next;
head=tmp;
}
}
pre_p->next = q->next;
return p->next;
}
};
Leetcode Partition List的更多相关文章
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [LeetCode] Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集
Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...
- [leetcode]Partition List @ Python
原题地址:https://oj.leetcode.com/problems/partition-list/ 题意: Given a linked list and a value x, partiti ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- LeetCode - Partition Labels
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- LeetCode: Partition List 解题报告
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- Leetcode: Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
随机推荐
- Linux SNMP oid
http://www.debianadmin.com/linux-snmp-oids-for-cpumemory-and-disk-statistics.html
- [LeetCode] Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- RTP/RTCP的时间同步机制
转自:http://blog.csdn.net/leesphone/article/details/5571972 RTP支持传送不同codec的steaming,不同codec的clock rate ...
- 轻松学习RSA加密算法原理
转自:http://blog.csdn.net/sunmenggmail/article/details/11994013 http://blog.csdn.net/q376420785/articl ...
- Python3 基本数据类型注意事项
Python3 基本数据类型 教程转自菜鸟教程:http://www.runoob.com/python3/python3-data-type.html Python中的变量不需要声明.每个变量在使用 ...
- linux脚本编程技术
linux脚本编程技术 一.什么是脚本 脚本是一个包含一系列命令序列的可执行(777)文本文件.当运行这个脚本文件时,文件中包含的命令序列将得到自动执行. 二.脚本编程 #!/bin/sh 首行固定格 ...
- 思想&观点&人生
思想: 思想的直接表现往往是对事物的观点,观点越多并且越接近本质,表示思想越丰富和深刻 观点不是事实 观点是基于事实之上的一种系统性的判断和理解框架,事实是观点的基础 观点不一定正确 观点组成: 事实 ...
- View(三)
大家在平时使用View的时候都会发现它是有状态的,比如说有一个按钮,普通状态下是一种效果,但是当手指按下的时候就会变成另外一种效果,这样才会给人产生一种点击了按钮的感觉.当然了,这种效果相信几乎所有的 ...
- 封装用className选元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...