Careercup - Google面试题 - 5735304249999360
2014-05-03 23:18
原题:
Insert a element in a sorted circular linked list
题目:题意简单明了,向一个有序的循环单向链表中插入元素,使得链表仍然有序。
解法:由于是循环链表,所以表尾指向表头。链表只能顺序访问,不额外添加数据的情况下就没法以对数的时间进行查找了。有几个边缘情况需要考虑:1. 链表为空 2. 插入到链表头 3. 插入到链表尾。考虑到各种可能情况,就能做出这题。
代码:
// http://www.careercup.com/question?id=5735304249999360
struct ListNode {
int val;
ListNode *next;
ListNode(int _val = ): val(_val), next(nullptr) {};
}; class Solution {
void insertElement(ListNode *&head, int new_val) {
if (head == nullptr) {
head = new ListNode(new_val);
head->next = head;
return;
} ListNode *ptr = nullptr;
if (new_val <= head->val) {
ptr = new ListNode(head->val);
ptr->next = head->next;
head->next = ptr;
head->val = new_val;
} else {
ListNode *prev = head;
ListNode *ptr2 = head->next;
while (ptr2 != head && ptr2->val < new_val) {
prev = prev->next;
ptr2 = ptr2->next;
}
ptr = new ListNode(new_val);
ptr->next = ptr2;
prev->next = ptr;
}
};
};
Careercup - Google面试题 - 5735304249999360的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
随机推荐
- HTML之背景颜色的改变
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- c#接口相关
public interface I接口名 { void SaiHi(); void Hello(string msg); //以上是方法 string Name { get; set; } //以上 ...
- 20141110--SQL视图
--------------------------视图--------------------- --视图是一个虚拟表,数据来自于原表,原表的数据改变,视图也会改变 select Student.S ...
- Js获取标签高度
能力有限:问个问题,标签相对页面高度,是怎么写? 鼠标的横坐标,X轴: event.clientX; 鼠标的竖坐标,Y轴: event.clientY; 网页可见区域宽: document.bo ...
- 【转载】#323 - A Generic Class is a Template for a Class
A generic classs is a class that takes one or more type parameters, which it then uses in the defini ...
- 通过Javascript数组设计一个省市联动菜单
通过Javascript数组设计一个省市联动菜单 使用Option内置类来完成下拉选项的创建 2.使用定时器实现一个时钟程序 3.使用PHP+JSON完成语音验证码 网址:http://yuyin.b ...
- 杭电ACM1170--Balloon Comes!
地址 http://acm.hdu.edu.cn/showproblem.php?pid=1170 #include<stdio.h> int main() { int t,a,b; ] ...
- Codevs 3231 小吃
时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 这里简直是吃货的天堂,小吃太多了.数不胜数. 假设岛上有N种小吃,每种体积Vi, ...
- [转]高并发访问下避免对象缓存失效引发Dogpile效应
避免Redis/Memcached缓存失效引发Dogpile效应 Redis/Memcached高并发访问下的缓存失效时可能产生Dogpile效应(Cache Stampede效应). 推荐阅读:高并 ...
- 推荐一些C#相关的网站、资源和书籍
一.网站 1.http://msdn.microsoft.com/zh-CN/ 微软的官方网站,C#程序员必去的地方.那里有API开发文档,还有各种代码.资源下载. 2.http://social.m ...