2.1  Write code to remove duplicates from an unsorted linked list

/* Link list node */
struct node
{
int data;
struct node* next;
};
void rem_duplicate(node *head){
if(NULL == head) return ;
set<int> hash;
set.insert(head->data);
while(head->next){
if(hash.find(head->next->data) == hash.end()){
node *tp = head->next;
head->next = tp->next;
delete tp;
}else{
hash.insert(head->next->data);
head = head ->next;
}
}
}

2.2 Implement an algorithm to fnd the nth to last element of a singly linked list

/* Link list node */
struct node
{
int data;
struct node* next;
};
node *nthToLast(node * head, int n){ if(NULL == head || n <) return NULL;
node *p;
p = head
for(int i = ; i < n; i++){
if(p== NULL) return NULL;
p = p->next;
}
node * q = head;
while(p->next){
p = p->next;
q = q->next;
}
return p;
}

2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node

/*
Initialize mid element as head and initialize a counter as 0. Traverse the list from head, while traversing increment the counter and change mid to mid->next whenever the counter is odd. So the mid will move only half of the total length of the list.
*/
/* Link list node */
struct node
{
int data;
struct node* next;
};
void deleteMiddle(struct node *head){
if(head == NULL) return ;
node * mid = head;
int count = 0;
while(head != NULL){
if(count & 1){
mid = mid->next;
}
count++;
head = head ->next;
} if(mid->next !=NULL)
{
mid->data = mid->next->data;
node *tp = mid->next;
mid->next = tp->next;
delete tp;
}else{
delete mid;
} }

  reference :http://www.geeksforgeeks.org/write-a-c-function-to-print-the-middle-of-the-linked-list/

2.4 You have two numbers represented by a linked list, where each node contains a single digit The digits are stored in reverse order, such that the 1’s digit is at the head of the list Write a function that adds the two numbers and returns the sum as a linked list

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(l1 == NULL) return l2;
if(l2 == NULL) return l1;
int last = ;
ListNode *p, *q, *pre;
p = l1; q= l2;
ListNode *head = p;
pre = NULL;
while(p && q){
p ->val += q->val + last;
if(p->val >= ){
last = ;
p->val -= ;
}else{
last = ;
}
pre = p;
p = p->next;
q = q->next;
}
p = NULL == q ? p : q;
pre ->next = p;
while(p && last == ){
p->val += last;
if(p->val >= ){
last = ;
p->val -= ;
}else{
last = ;
}
pre = p;
p = p->next;
}
if(last == ){
q = new ListNode();
pre ->next = q;
}
return head ;
}
};

2.5 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop 

DEFINITION
Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an
earlier node, so as to make a loop in the linked list
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C

分析:最简洁易懂的还是数学形式的表达。设有两个指针p1、p2, p1每次向前移动一下,p2每次向前移动两个。p1到达loop 入口时,p2比p1多走了K个节点(k即为从链表入口到loop 的距离),假设T时刻两个节点相遇,则2T - T = n - k  即 T = n - K ,即相遇点到loop的入口距离为K。那么相遇点到Loop 的距离和链表头到loop 的距离相等,loop的入口点可求。

/* Link list node */
struct node
{
int data;
struct node* next;
};
// 假设输入的链表一定有环
node * FindBeginning(node * head){ if(head == NULL) return NULL;
node *p, *q;
p = head ; q = head;
do{
p = p->next;
q = q->next;
q = q->next;
}while(p != q) q = head ;
while(q != p){
p = p->next;
q = q->next ;
}
return p;
}

CCI_chapter 2 Linked Lists的更多相关文章

  1. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  3. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  4. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  5. Leetcode 160. Intersection of two linked lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  6. (LinkedList)Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. Java for LeetCode 160 Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  8. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  9. LeetCode Intersection of Two Linked Lists

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...

随机推荐

  1. 【转】ActionBar的基本用法

    原文网址:http://irtutsk.iteye.com/blog/2117707 ActionBar的组成: [1]AppIcon:标题区,显示应用程序图标和标题,也可以自定义. [2]ViewC ...

  2. HDU4432 Sum of Divisors

    涉及知识点: 1. 进制转换. 2. 找因子时注意可以降低复杂度. Sum of divisors Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  3. CSS常用操作-分类

  4. [深入react] 4.牛逼闪闪的虚拟DOM

    React.createElement嵌套后的结果就是虚拟dom,虚拟dom听着很高端,其实就是一个json,类似: { type:'div', props:{ className:"box ...

  5. Sequence(组合数学,集合不同元素的个数)

    Sequence [组合数学] 时间限制: 3 Sec  内存限制: 128 MB 提交: 138  解决: 52 [提交][状态][讨论版] 题目描述 在某个夜黑月高的晚上,!!!,原谅我编不下去了 ...

  6. 基于年纪和成本(Age & Cost)的缓存替换(cache replacement)机制

    一.客户端的缓存与缓存替换机制 客户端的资源缓存: 在客户端游戏中,通常有大量的资源要处理,这些可能包括贴图.动作.模型.特效等等,这些资源往往存在着磁盘文件->内存(->显存)的数据通路 ...

  7. 10、Cocos2dx 3.0游戏开发找小三之容器篇:Vector、Map、Value

    重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27705613 容器 3.0版本号之前Cocos2d- ...

  8. form表单提交之前推断

    1.使用onsubmit方法 <form name="Form" action="t" method="post" onsubmit= ...

  9. 50个android开发技巧

    50个android开发技巧 http://blog.csdn.net/column/details/androidhacks.html

  10. Mysqldb连接Mysql数据库(转)

    python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库 ...