[002] delete_duplication_of_linked_list
[Description] Given a unsort linked list, delete all the duplication from them, no temporary space permission.
[Thought] Set two points, from head to tail, scaning all linked list. O(n^2).
[Implementation] C code:
#include<stdio.h> typedef struct linkedList
{
int data;
struct linkedList* next;
} llist; llist** delDupli(llist **head)
{
// two point, t1 for target, t2 for mover.
llist *t1, *t2;
t1=t2=*head;
while(t1->next != NULL)
{
// ajust t2 from t1!
t2=t1;
while(t2->next != NULL)
{
// check if they are the same
if(t2->next->data == t1->data)
{
// move t2->next; note: no memory free here.
t2->next=t2->next->next;
}
// after move out node, check if t2 is already the last one.
if(t2->next != NULL)
{
t2=t2->next;
}
}
t1=t1->next;
}
return head;
} int addValue(llist **head, int val)
{ llist *pnode;
llist *t=*head;
// apply memory for linked list.
pnode=(llist*)malloc(sizeof(llist));
if(NULL==pnode)
{
return ;
}
pnode->data=val;
pnode->next=NULL;
// first node
if(NULL==*head)
{
*head=pnode;
return ;
}
// move temp point to the last node
while(t->next != NULL)
{
t=t->next;
}
t->next=pnode;
return ;
} // out put data, be careful about the first node.
int printList(llist **head)
{
llist *t=*head;
printf("%d, ",t->data);
while(t->next != NULL)
{
t=t->next;
printf("%d, ",t->data);
}
printf("\n");
return ;
} int main()
{
int a[]={,,,,,,,,,,,,};
llist *head=NULL;
int i; for(i=;i<(sizeof(a)/sizeof(a[]));i++)
{
// printf("a[i]:%d\n",a[i]);
addValue(&head,a[i]);
// printList(&head);
}
printf("Initial linked list:\n");
printList(&head);
printf("After deleting:\n");
printList(delDupli(&head));
}
[002] delete_duplication_of_linked_list的更多相关文章
- 【GoLang】GO语言系列--002.GO语言基础
002.GO语言基础 1 参考资料 1.1 http://www.cnblogs.com/vimsk/archive/2012/11/03/2736179.html 1.2 https://githu ...
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数002·AI人工智能
<zw版·Halcon-delphi系列原创教程> Halcon分类函数002·AI人工智能 AI人工智能:包括knn.gmm.svm等 为方便阅读,在不影响说明的前提下,笔者对函数进行了 ...
- php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动
php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动 每个人机器不一样,我手头是个air book,查了一下现在最好在mac下,用mamp, mamp百科介绍 , ...
- 【面试题002】java实现的单例模式,c++实现单例模式,实现禁止拷贝
[面试题002]java实现的单例模式,c++实现单例模式,实现禁止拷贝 一 c++实现单例模式 保证一个类,在一个程序当中只有一个对象,只有一个实例,这个对象要禁止拷贝,注意这里要区别于java. ...
- [反汇编练习] 160个CrackMe之002
[反汇编练习] 160个CrackMe之002. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 002 Spring Restful案例
1:工程结构 需要注意的是需要额外导入以下三个包: jackson-annotations-2.6.1.jar jackson-core-2.6.1.jar jackson-databind-2.6. ...
- python----特性002
python特性002:特性是可继承的 #!/usr/local/python3.5/bin/python3 class Person(object): def __init__(self,name) ...
- python解释器内建函数002
001.dict 函数来创建字典 #!/usr/bin/python #!coding:utf-8 if __name__ == "__main__": dct001=dict(h ...
- Python[小甲鱼-002用Python设计第一个游戏]
–Code——————————————————————- print("----------第一个小游戏----------") temp = input("猜一下我现在 ...
随机推荐
- BZOJ4878 挑战NP-Hard(dfs树)
既然是二选一,考虑两个问题有什么联系.题面没有说无解怎么办,所以如果不存在经过k条边的简单路径,一定存在k染色方案.考虑怎么证明这个东西,我们造一棵dfs树.于是可以发现如果树深>k(根节点深度 ...
- BZOJ1089 [SCOI2003]严格n元树 【dp + 高精】
Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该树中最底层的节点深度为d (根的深度为0),那么我们称它为一棵深度为d的严格n元树.例如,深度为2的严 ...
- Linux(三)高级文本处理命令
一.cut (cut 命令可以从一个文本文件或者文本流中提取文本列 ) 1.cut语法 cut -d '分隔字符' -f fields 用于有特定分隔字符 cut -c 字符区间 ...
- Codeforces 582C. Superior Periodic Subarrays(数学+计数)
首先可以把 i mod n=j mod n的看成是同一类,i mod s=j mod s的也看成是同一类,也就是i mod gcd(s,n)的是同一类,很好理解,但是不会数学证明...大概可以想成数轴 ...
- Linux常用网络工具:fping主机扫描
Linux下有很多强大网络扫描工具,网络扫描工具可以分为:主机扫描.主机服务扫描.路由扫描等. fping是一个主机扫描工具,相比于ping工具可以批量扫描主机. fping官方网站:http://f ...
- Centos7单机部署ELK
一. 简介 1.1 介绍 ELK是三个开源工具组成,简单解释如下: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风 ...
- [吴恩达机器学习笔记]12支持向量机5SVM参数细节
12.支持向量机 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考资料 斯坦福大学 2014 机器学习教程中文笔记 by 黄海广 12.5 SVM参数细节 标记点选取 标记点(landma ...
- mysql 查询小demo
两张表的的结构如下,需求是写出从one表到two表和从two表到one表的查询转换. create table student_one( name varchar(50) default '' not ...
- JS笔记加强版3
JS 面向对象及组件开发 JS的面向对象: 1.什么是面向对象编程 用对象的思想去写代码,就是面向对象编程 过程式写法 面向对象写法 我们一直都在使用对象 数组 Array 时间 Date ...
- IDEA调试服务器上部署的程序
提出问题: 一个程序,部署在自己的电脑上,debug调试,相信大家都会,但是,如果我想debug调试非本地部署的程序怎么办呢.比如测试服务器上部署的程序. 其实这样的需求也是经常有的,比如一个大型的项 ...