[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("猜一下我现在 ...
随机推荐
- git 恢复单个文件
首先查看该文件的历史版本信息:git log Default@2x.png 记录下需要恢复的commit版本号:如 9aa51d89799716aa68cff3f30c26f8815408e926 恢 ...
- 【比赛】HNOI2018 总结
一将功成万骨枯,我就是给那些队爷做基数的 一.比赛过程 Day1 看完题,暴力好打,然后就打 觉得第三题模型很好建啊,先看第三题吧(结果第三题是最..的) 图建出来,先看树的情况,设dp试一下 结果一 ...
- 关于Centos的yum安装LAMP
Step1.配置网易的源 在mirrors.163.com中找到centos的帮助信息,完成源的更新配置,如图1,图2,图3所示. 图1 图2 图3 Step2.然后安装PHP+MySQL+apach ...
- 【51Nod1386】双马尾机器人Description 解题报告
[51Nod1386]双马尾机器人Description 给定\(n\)和\(k\),我们要在\(1,2,3,...,n\)中选择若干的数,每一种选择的方案被称为选数方案. 我们定义一种选数方 ...
- sysbench - 单组件式测试工具
1 安装 > ./configure --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/my ...
- 【learning】加权拟阵与贪心
首先.. 这篇东西的话算是一个关于拟阵部分知识的小总结,有些语言相对来说偏向便于理解方面,所以可能..有一些说法会不是那么严谨大概是这样 一些概念 线性无关:一组数据中没有一个量可以写成其余量的线 ...
- 使用log4net将日志文件输出为csv格式
我们在编写程序时,会在程序运行过程中记录一些日志.log4net作为一款经久耐用的日志组件,值得我们信赖.在中小型公司中,往往没有专业的日志服务器来处理应用程序产生的日志,而格式化不好的日志文件又为上 ...
- Java之面向对象编程20170619
/*************************************************************************************************** ...
- 找圆算法((HoughCircles)总结与优化
http://www.opencv.org.cn/forum.php?mod=viewthread&tid=34096 Opencv内部提供了一个基于Hough变换理论的找圆算法,Hough ...
- 图像RGB格式与YUV格式互转
// rgb2yuv.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #define Level 256 //直方图bin数 #define ...