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 ...
随机推荐
- BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏
3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 47 Solved: 37[ ...
- 【转】用串口登录Beaglebone Black、用usb共享电脑网络、内核模块的本地编译
原文网址:http://bbs.eeworld.com.cn/thread-431507-1-1.html 串口连接BBB使用usb线可以连接BBB和电脑,用ssh就可以登录BBB来进行操作.但有时候 ...
- 2015第28周六SVN和Git
svn作为一个优秀源码版本的管理工具,可以适合绝大多数项目.但是因为它的采用中心化管理,不可避免的存在本地代码的备份和版本管理问题.也就是说对于尚未或暂无法提交到Subversion服务器的本地代码来 ...
- Android软件的国际化
软件的国际化指的就是:在不同语言的环境的操作系统下,显示不同的语言 2 其实实现软件的国际化很简单: 3 4 1.如果是对文字的国际化,只需要在res文件夹下面建立如下文件夹: 5 values-zh ...
- 使用strace查看C语言级别的php源码
XCACHE XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接 ...
- markdown转dokuwiki
markdown markdown格式变得越来越通用,很多博客,云笔记,github等等都支持markdown编写,markdown也能很方便的转化为pdf,html等格式.公司的文档用的dokuwi ...
- Java虚拟机之垃圾回收详解一
Java虚拟机之垃圾回收详解一 Java技术和JVM(Java虚拟机) 一.Java技术概述: Java是一门编程语言,是一种计算平台,是SUN公司于1995年首次发布.它是Java程序的技术基础,这 ...
- android后台截屏实现(1)--源码编译
前段时间接到任务要实现后台截图并上传的功能,在网上查了好久,发现遇到这类问题的人还不少.经过一番对比后发现还是修改并编译源码中的screencap类然后通过JNI来调用这种方法比较可靠,而其他的在ja ...
- js判断上传文件大小
下面提供三款网页特效判断上传文件大小哦,这三种方法是现在限制文件上传大小比较好的方法,可以在客户上传文件时限制上传文件大小判断处理<!doctype html public "-//w ...
- smbpasswd命令常用选项
smbpasswd命令的常用方法 smbpasswd -a 增加用户(该账户必须存在于/etc/passwd文件中)smbpasswd -d 冻结用户,就是这个用户不能在登录了smbpasswd -e ...