careercup-链表 2.4
2.4 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前。
思路:将小于的结点还是保存在原来的链表中,将大于等于x的结点加入一个新的链表,最后将这两个链表链接起来。
C++实现代码:
#include<iostream>
#include<new>
using namespace std; struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL) {}
}; void createList(ListNode *&L)
{
int arr[]= {,,,,,,,,,};
int i;
ListNode *p=NULL;
for(i=; i<; i++)
{
ListNode *tmp=new ListNode(arr[i]);
if(L==NULL)
{
L=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=tmp;
}
}
} void splitX(ListNode *L,int x)
{
if(L==NULL)
return;
ListNode *pre=L;
ListNode *p=L;
ListNode *L1=NULL;
ListNode *q=NULL;
while(p)
{
if(p->val<x)
{
pre=p;
p=p->next;
}
else
{
ListNode *tmp=p;
p=tmp->next;
pre->next=p;
tmp->next=NULL;
if(L1==NULL)
{
L1=tmp;
q=tmp;
}
else
{
q->next=tmp;
q=tmp;
}
}
}
pre->next=L1;
} int main()
{
ListNode *head=NULL;
createList(head);
ListNode *p=head;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
splitX(head,);
p=head;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
careercup-链表 2.4的更多相关文章
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项
2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...
- [CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素
2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元 ...
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
- [CareerCup] 2.4 Partition List 划分链表
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表
4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each de ...
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST
引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...
- Careercup - Google面试题 - 5735304249999360
2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...
随机推荐
- oracle 报“无效数字”异常和“ORA-01830: 日期格式图片在转换整个输入字符串之前结束”
1.问题1 执行下列SQL: sql = "select count(1) as totle from vhl_model_data a where a.OBTAIN_CREATE_TIME ...
- Hbase案例分析(一)
Hbase应用场景: 1 随机读或者写 2 大数据上的高并发操作,比如每秒对PB级数据进行上千次操作.(查询,删除等操作) 3 读写均是非常简单的操作,比如没有join操作 Hbase Schema设 ...
- Python连接Redis连接配置
1. 测试连接: Python 2.7.8 (default, Oct 20 2014, 15:05:19) [GCC 4.9.1] on linux2 Type "help", ...
- Java运算符(一)equals方法与“==”
超类Object的equals只是比较两者之间的引用对象是否相同,这一点跟操作符“==”是一样的. 在基本数据类型中,“==”用于比较两者之间的值(内容)是否相等. 在引用类型中,“==”用于比较两者 ...
- Linux数据管理——文件锁定
一.什么是文件锁定 对于锁这个字,大家一定不会陌生,因为我们生活中就存在着大量的锁,它们各个方面发挥着它的作用,现在世界中的锁的功能都可归结为一句话,就是阻止某些人做某些事,例如,门锁就是阻止除了屋主 ...
- Default Web Site
win7上设置默认website的物理路径
- C++ Prime:范围for语句
C++11新标准引入了一种更简单的for语句,这种语句可以遍历容器或者其他序列的所有元素.范围for语句的语法形式是: for( declaration : expression) statement ...
- BZOJ1510: [POI2006]Kra-The Disks
1510: [POI2006]Kra-The Disks Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 265 Solved: 157[Submit][ ...
- 【转】iOS开发者账号和证书
原文网址:http://www.jianshu.com/p/8e967c1d95c2 从Xcode7之后,苹果支持了免证书调试,但是若是需要调试推送功能,或者需要发布App,则需要使用付费的开发者账户 ...
- Centos6.4 设置开机自动以某个非root用户启动脚本
开机自动运行脚本,可以将脚本的执行命令放在 /etc/rc.d/rc.local 文件中,但是这样开机自动运行这个脚本的用户默认为root. 如果想以某个非root用户运行脚本,可以使用如下命令: s ...