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的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

  3. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  4. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  5. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  6. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  7. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  8. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  9. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

随机推荐

  1. Android studio 安装和使用

    之前一直是使用eclipse+ADT+SDK进行Android的开发的,不愿意转到Android studio是因为熟悉了eclipse的开发环境,最近偶然使用了android studio,感觉比使 ...

  2. CSS之页面添加标签

    就是因为昨天弄这个“神奇的小标签”差点把自己的园子给废了(情节真的有这么严重),说多了都是泪啊~~(┳_┳).本来是想在页首添加这个“神奇的小标签”的,不知是插件有BUG还是代码错误当场就导致不能编辑 ...

  3. MVC 增加手机站

    首先在全局路由(RouteConfig)中加入以下图片中的代码.

  4. 20141103--SQL主键,外键

    设置主键: 右键表→设计,在需要的列上右键→设置主键 或者在创建表的时候,在想要作为索引的列后面加上 primary key create table biao3 ( [No.] int primar ...

  5. JavaScript图片轮播器

    先贴上html的代码 <div class="ImgDiv"> <div class="Imgs" id="imgScroll&qu ...

  6. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

  7. 使用JDBC从数据库中查询数据

    * ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回 ...

  8. MySQL 性能优化

    内容简介:这是一篇关于mysql 性能,mysql性能优化,mysql 性能优化的文章.网上有不少mysql 性能优化方案,不过,mysql的优化同sql server相比,更为麻烦与负责,同样的设置 ...

  9. html关于强制显示 隐藏浏览器的滚动条

    浏览器的滚动条在一些特殊的展示中,是不需要的,所以必须把它隐藏掉,文章主要介绍一些隐藏或者显示IE的水平或者垂直滚动条的实现代码,需要了解的朋友可以参考下: 相关css代码如下: //强制显示滚动条: ...

  10. ECMAScript整理笔记(持续更新....)

    参考文献: ECMAScript Array:http://www.jimmycuadra.com/posts/ecmascript-5-array-methods ECMAScript5兼容展示大全 ...