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 ...
随机推荐
- bootstrap table 保留翻页选中数据
$(function () { $('#exampleTable').on('uncheck.bs.table check.bs.table check-all.bs.table uncheck-al ...
- Bootstrap 原始按钮
Bootstrap 原始按钮 <!DOCTYPE html><html><head><meta http-equiv="Content-Type&q ...
- shell脚本,alias别名命令用法。
[root@localhost ~]# alias alias cp='cp -i' alias mv='mv -i' alias rm='rm -i' [root@localhost ~]# [ro ...
- iOS开发遇见的坑之二:工程文件中插件和自身工程命名冲突
在升级cocoapod后,我重新管理了一下工程,其实也就是把各个类分类进行管理 类似于这样 然后编译就发现不能运行 1.其中一个错误是工程文件缺失,根据提示添加进来进行 2.有一个是pch的相对路径变 ...
- vue-router介绍及简单使用
一.vue-router介绍 vue-router是vue官方提供的一个路由框架,控制页面路由,使用较为方便. 1.路由模式 hash(浏览器环境默认值),使用 URL hash 值来作路由,支持所有 ...
- 简单的Redis数据迁移
dump迁移 1.安装redis-dump工具 sudo apt-get install ruby rubygems ruby-devel -y gem sources --add http://ge ...
- 【Java_基础】java中的多态性
方法的重载.重写和动态链接构成了java的多态性. 1.方法的重载 同一个类中多个同名但形参有所差异的方法,在调用时会根据参数的不同做出选择. 2.方法的重写 子类中重新定义了父类的方法,有关方法重写 ...
- 【实用工具】Teleport Pro爬取整个网站镜像到本地
1. 使用Teleport Pro可以完全或部分下载一个网站上的内容,在硬盘上创建一个与原网站完全相同的镜象,使用户能够离线浏览 Teleport Pro的安装以及基本使用 在菜单栏Project下得 ...
- python:json
json是用来传输数据的字符串,{"key1":"values1","key2":{"key3":"value ...
- JAVA面向过程VS面向对象
面向过程 面向过程是一种自顶向下的编程,强调行为过程,可扩展性可维护性差. 优点: 性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源. 单片机.嵌入式开发.Linux/Unix等一般 ...