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. 【HDOJ】4515 小Q系列故事——世界上最遥远的距离

    简单题目,先把时间都归到整年,然后再计算.同时为了防止减法出现xx月00日的情况,需要将d先多增加1,再恢复回来. #include <cstdio> #include <cstri ...

  2. 《SDN核心技术剖析和实战指南》2.1交换机核心技术小结

    对于SDN交换机的技术,其实也适用于传统的交换机,只不过控制部分被分离出来而已.传统交换机的控制面主要是转发表的管理以及网络状态之类的各种表,现在这些都由控制器来担心.转发面主要由转发决策.背板和输出 ...

  3. jetty插件配置

    1.jetty maven 插件启动设置: Base directory:${project_loc} Goals:clean -Djetty.port=8080 jetty:run 2.jetty ...

  4. ETL-Career RoadMap

    RoadMap: 1.Tester:sql的单体或批处理测试: 2. Application Developer 2.1 批处理手动工具(如何使用.如何调度批处理.如何生成批处理脚本): 2.2 批处 ...

  5. 跟我一起学extjs5(22--模块Form的自己定义的设计)

    跟我一起学extjs5(22--模块Form的自己定义的设计)         前面几节完毕了模块Grid的自己定义,模块Form自己定义的过程和Grid的过程类似,可是要更复杂一些.先来设计一下要完 ...

  6. 函数返回char* 的解决方案

    在C语言中,自动变量在堆栈中分配内存.当包含自动变量的函数或代码块退出时,它们所占用的内存便被回收,它们的内容肯定会被下一个所调用的函数覆盖.这一切取决于堆栈中先前的自动变量位于何处,活动函数声明了什 ...

  7. 常用的JS数据类型转换方法

    JS 数据类型转换的方法有以下3种:1)使用转换函数2)强制类型转换3)利用js变量弱类型特性进行转换 1:js提供了parseInt()和parseFloat()这两个转换函数. 这里输入内容par ...

  8. 理解JavaScript中作用域链的关系

    javascript里的关系又多又乱.作用域链是一种单向的链式关系,还算简单清晰:this机制的调用关系,稍微有些复杂:而关于原型,则是prototype.proto和constructor的三角关系 ...

  9. jQuery下的显示和隐藏

    因为太久没更新了,所以来放一点没意思的内容. 做的是jQuery框架的隐藏和显示,HTML如下: <ul> <li>1</li> <li>2</l ...

  10. 自动生成 Lambda查询和排序,从些查询列表so easy

    如下图查询页面,跟据不同条件动态生成lambda的Where条件和OrderBy,如果要增加或调整查询,只用改前台HTML即可,不用改后台代码 前台代码: <div style="pa ...