CCI_chapter 2 Linked Lists
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的更多相关文章
- [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 ...
- 【leetcode】Intersection of Two Linked Lists
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 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 ...
- (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 ...
- 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 ...
- [leetCode][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
随机推荐
- 如何新建XCode项目
一.IOS的基础知识 1.只有一个应用程序正在运行.在IOS上,每一段时间内只能激活一个应用程序并在屏幕上显示. 2.只有一个窗口.只允许应用程序操作的一个窗口. 3.访问受限.只能在IOS为应用程序 ...
- 执行npm安装模块的命令 Cannot find module
npm 安装了 appium 和 appium-doctor 运行命令,appium-doctor 提示找不到模块: C:\Users\autotest>appiummodule.js:471 ...
- HTML5 简单实现刮刮乐效果
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- if(!!attr)是什么鬼???
看到很多代码if(!!attr),为什么不直接写if(attr):其实这是一种更严谨的写法:请测试 typeof 5和typeof !!5的区别.!!的作用是把一个其他类型的变量转成的bool类型.
- sublime3 使用技巧
Ctrl+O(Command+O)可以实现头文件和源文件之间的快速切换 Ctrl+Shift+T可以打开之前关闭的tab页,这点同chrome是一样的 Ctrl+R定位函数:Ctrl+G定位到行: 插 ...
- iOS 时区问题总结 NSTimeZone
基本概念 GMT 0:00 格林威治标准时间; UTC +00:00 校准的全球时间; CCD +08:00 中国标准时间 [来自百度百科] 夏时制,英文"DaylightSavingTim ...
- 微信中web页面实现和公众号中查看图片一样的效果
最近开发了一套资讯相关的web页面,嵌套在微信中,可支持点赞.评论等...在文章详情中,图片需要点击放大,随手势放大缩小,左右可滑动切换,总之类似于微信公众号效果. 开始想的方案是用轮播插件.或者在i ...
- [MySQL CPU]线上飙升800%,load达到12的解决过程
接到报警通知,负载过高,达到800%,load也过高,有11了. MySQL版本号为5.6.12-log 1 top 之后,确实是mysqld进程占领了全部资源. 2 查看error日志,无不论什么异 ...
- 查询rman备份信息经常使用指令
查询rman备份信息经常使用指令 ----登陆到rman $rman target / ----以精简的格式查看备份信息 RMAN> list backup of database summar ...
- Qt Creator实现状态栏显示
在程序主窗口Mainwindow中,有菜单栏,工具栏,中心部件和状态栏.前面几个已经讲过了,这次讲解状态栏的使用. 程序中有哪些不明白的类或函数,请自己查看帮助. 1.我们在mainwindow.h中 ...