《Cracking the Coding Interview》——第2章:链表——题目4
2014-03-18 02:27
题目:将一个单链表按照一个值X分为两部分,小于X的部分放在大于等于X的部分之前。
解法:按照值和X的大小,分链表为两条链表,然后连起来成一条。
代码:
// 2.4 Write code to partition a linked list around a value x, such that all nodes less than x comes before all nodes greater than or equal to x.
#include <cstdio>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(nullptr) {};
}; class Solution {
public:
ListNode* partitionList(ListNode *head, int x) {
if (head == nullptr) {
return head;
} ListNode *h1, *t1, *h2, *t2;
ListNode *ptr; h1 = t1 = nullptr;
h2 = t2 = nullptr;
ptr = head;
while (ptr != nullptr) {
if (ptr->val < x) {
if (h1 == nullptr) {
h1 = t1 = ptr;
} else {
t1->next = ptr;
t1 = t1->next;
}
ptr = ptr->next;
t1->next = nullptr;
} else {
if (h2 == nullptr) {
h2 = t2 = ptr;
} else {
t2->next = ptr;
t2 = t2->next;
}
ptr = ptr->next;
t2->next = nullptr;
}
}
if (h1 == nullptr) {
return h2;
} else if (h2 == nullptr) {
return h1;
} else {
t1->next = h2;
return h1;
}
}
}; int main()
{
int i;
int n, x;
int val;
struct ListNode *head, *ptr;
Solution sol; while (scanf("%d", &n) == && n > ) {
// create a linked list
ptr = head = nullptr;
for (i = ; i < n; ++i) {
scanf("%d", &val);
if (head == nullptr) {
head = ptr = new ListNode(val);
} else {
ptr->next = new ListNode(val);
ptr = ptr->next;
}
} // partition the list around value x.
scanf("%d", &x);
head = sol.partitionList(head, x); // print the list
printf("%d", head->val);
ptr = head->next;
while (ptr != nullptr) {
printf("->%d", ptr->val);
ptr = ptr->next;
}
printf("\n"); // delete the list
while (head != nullptr) {
ptr = head->next;
delete head;
head = ptr;
}
} return ;
}
《Cracking the Coding Interview》——第2章:链表——题目4的更多相关文章
- Cracking the Coding Interview:: 寻找有环链表的环路起始节点
给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果 ...
- Cracking The Coding Interview 2.0 单链表
#include <iostream> #include <string> using namespace std; class linklist { private: cla ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第2章:链表——题目7
2014-03-18 02:57 题目:检查链表是否是回文的,即是否中心对称. 解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比.对比完了再将后半条反转了拼回去.这样不涉及额 ...
- 《Cracking the Coding Interview》——第2章:链表——题目6
2014-03-18 02:41 题目:给定一个带有环的单链表,找出环的入口节点. 解法1:用hash来检测重复节点肯定是容易想而且效率也高的好办法. 代码: // 2.6 You have a ci ...
随机推荐
- 笨办法学Python(十五)
习题 15: 读取文件 你已经学过了 raw_input 和 argv,这些是你开始学习读取文件的必备基础.你可能需要多多实验才能明白它的工作原理,所以你要细心做练习,并且仔细检查结果.处理文件需要非 ...
- js 关系运算符
1.大于 > (小于 效果一样) > //true > //false //false,如果有一个字符串,字符串转换成数值在比较 ' //true,如果两个都是字符串,则比较第 ...
- iRecognizer号码扫描开发实录
iRecognizer——这是一款可以帮助你快速扫描获得印刷体数字的软件 现已上架 腾讯应用宝,酷安 提供的功能:扫一扫(相册或当场扫描),获得电话号码,之后就可以拨打或者发送短信,自动复制到剪贴板, ...
- cesium 加载倾斜摄影模型(这里有一坑)
代码如下: // Construct the default list of terrain sources. var terrainModels = Cesium.createDefaultTerr ...
- node入口文件分析和目录初始化
1.需要安装的模块 npm install express npm install jade npm install mongoose npm install bower -g npm install ...
- 在TextBox控件中禁用鼠标右键
实现效果: 知识运用: MouseEventArgs类的Button属性 TextBox控件的ContextMenu属性 实现代码: private void textBox1_MouseDo ...
- C++STL之multiset多重集合容器
multiset多重集合容器 multiset与set一样, 也是使用红黑树来组织元素数据的, 唯一不同的是, multiset允许重复的元素键值插入, 而set则不允许. multiset也需要声明 ...
- 剑指offer 和为s的两个数字的调试
这是整个调试, for (int i:s) cout<<i<<endl;这句话是c++11特性下的一种遍历方式 在编译的时候需要加-std=c++11,即g++ 41.cpp ...
- PHP中json_encode后,在json字符串中依然显示中文的解决方案
<?php header("Content-Type:text/html;charset=utf-8;"); $arr = array ('Version_code'=> ...
- 基于LNMP环境的ssh2扩展
openssl: 加密算法集合,C语言实现 libssh2:ssh2协议库库,C语言实现 PECL/ssh2: libssh2的php扩展,允许php程序调用libssh2中的函数 依赖关系:PECL ...