LeetCode OJ--Partition List
http://oj.leetcode.com/problems/partition-list/
链表的处理
#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode *head1 = NULL,*head2 = NULL,*tail1 = NULL,*tail2 = NULL,*p = NULL;
p = head;
bool flag1 = ,flag2 = ;
while(p)
{
if(p->val<x)
{
if(flag1 == )
{
head1 = tail1 = p;
flag1 = ;
}
else
{
tail1->next = p;
tail1 = p;
}
}
else
{
if(flag2 == )
{
head2 = tail2 = p;
flag2 = ;
}
else
{
tail2->next = p;
tail2 = p;
}
}
p = p->next;
}
if(head1 == tail1 && tail1 == NULL)
return head2;
tail1->next = head2;
if(tail2)
tail2->next = NULL;
return head1; }
}; int main()
{
ListNode *n1 = new ListNode();
ListNode *n2 = new ListNode();
ListNode *n3 = new ListNode();
ListNode *n4 = new ListNode();
ListNode *n5 = new ListNode();
ListNode *n6 = new ListNode();
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
n5->next = n6;
ListNode *ans;
Solution myS;
ans = myS.partition(NULL,);
return ;
}
LeetCode OJ--Partition List的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- Linux部署多个tomcat
Linux部署多个tomcat 1.环境:1.1. Centos 5.01.2.Tomcat 5.5.17 2.需要解决一下几个问题2.1.不同的tomcat启动和关闭监听不同的端口2.2.不同的to ...
- XDB基于Library的备份及恢复
基于standalone全备份 语句: xdb backup --federation xhive://localhost:1235 --standalone --file E:\xdbData\xD ...
- CSS3-媒体类型
一.媒体类型(Media Type) 1.link方法引入 <link rel="stylesheet" type="text/css" href=&qu ...
- verilog RTL 编程实践之五
How to build and test a module 1.test have: generate .stimulus .check .respose 2.only one monitor ca ...
- 【curl】【php】curl报错,错误代码77,CURLE_SSL_CACERT_BADFILE (77)解决方法
CURLE_SSL_CACERT_BADFILE (77) - 读取 SSL CA 证书时遇到问题(可能是路径错误或访问权限问题) 在微信接口相关开发时容易出现此问题 这一般是因为服务更新了相关的软件 ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题
The Heaviest Non-decreasing Subsequence Problem 解题心得 这个题就是一个简单的动态规划,非递减最长子序列的改版(加一个权重),只要把权重为5的改成5个权 ...
- sublime__最全面的 Sublime Text 使用指南
感谢大佬--> 原文链接 摘要(Abstract) 本文系统全面的介绍了Sublime Text,旨在成为最优秀的Sublime Text中文教程. 前言(Prologue) Sublime T ...
- python基础——14(shelve/shutil/random/logging模块/标准流)
一.标准流 1.1.标准输入流 res = sys.stdin.read(3) 可以设置读取的字节数 print(res) res = sys.stdin.readline() print(res) ...
- win7定时关机
菜单>附件>系统工具>任务计划程序>创建基本任务 alt+r>cmd>shutdown/? 查看相关参数 /l 注销 /s 关机 /r 重启 /g 重启,重启后,重 ...
- luogu2216 [HAOI2007]理想的正方形
先对于每一行中长度为 n 的列用单调队列搞出它们的最小/大值,再将这些长度为 n 的列想象成点再对行跑一遍 #include <iostream> #include <cstring ...