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 ...
随机推荐
- 继承Animation
package cativity.cyq.alphaanimal; import android.view.animation.Animation; import android.view.anima ...
- Android支付宝SDK开发笔记
一.准备工作 〉1.下载开发包 https://b.alipay.com/order/productDetail.htm?productId=2014110308141993&tabId=4# ...
- Part 3 ViewData and ViewBag in mvc
ViewBag and ViewData is a mechanism(机制) to pass data from controller to view. We use '@' symbol(符号) ...
- C# 线程抛异常
异常抛出 异常抛出要在线程代码中抛出,否则捕获不到 using System; using System.Threading; namespace testthread_keyword_lock { ...
- 备忘-zTree v3.5 Demo 演示
zTree v3.5 Demo 演示: http://www.ztree.me/v3/demo.php#_110
- IOS自定义场景切换动画。
IOS中我们可以通过Storyborad以及segue来实现我们自己的场景切换动画,新建项目使用Single View Application模板并取名为MyCustomSegue. 使用storyb ...
- 10款让人惊叹的HTML5/jQuery图片动画特效
1.HTML5相册照片浏览器 可连接Flickr照片服务 以前我们经常会分享一些jQuery相册浏览插件,效果不错,实用性也很强.不过如果能利用HTML5来实现相册浏览器,那么相册浏览效果肯定会更加炫 ...
- Android下各个按键对应的key code
KEYCODE_UNKNOWN=0; KEYCODE_SOFT_LEFT=1; KEYCODE_SOFT_RIGHT=2; KEYCODE_HOME=3; KEYCODE_BACK=4; KEYCOD ...
- eclipse 最全快捷键(网络收集)
Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...
- android-support-xxxx.jar NoClassDefFoundError
当你的项目出现以下红色提示的时候,要小心了,因为很可能因为这个错误而导致解释不通的异常出现. Found 2 versions of android-support-v4.jar in the dep ...