[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("猜一下我现在 ...
随机推荐
- Using the command line to manage files on HDFS--转载
原文地址:http://zh.hortonworks.com/hadoop-tutorial/using-commandline-manage-files-hdfs/ In this tutorial ...
- C++解析-外传篇(2):函数的异常规格说明
0.目录 1.异常规格说明 2.unexpected() 函数 3.小结 1.异常规格说明 问题: 如何判断一个函数是否会抛出异常,以及抛出哪些异常? C++提供语法用于声明函数所抛出的异常 异常声明 ...
- [CTSC2012]熟悉的文章 后缀自动机
题面:洛谷 题解: 观察到L是可二分的,因此我们二分L,然后就只需要想办法判断这个L是否可行即可. 因为要尽量使L可行,因此我们需要求出对于给定L,这个串最多能匹配上多少字符. 如果我们可以对每个位置 ...
- ueditor上传图片配置
1 去ueditor文件夹下 找 ueidtors/dialogs/image/image.html -- 配置位置大概如下: 107 utils.domReady(function(){ 108 ...
- Codeforces 894.D Ralph And His Tour in Binary Country
D. Ralph And His Tour in Binary Country time limit per test 2.5 seconds memory limit per test 512 me ...
- socket--粘包
参考博客:http://www.cnblogs.com/kex1n/p/6502002.html 一.粘包现象 在上一篇的socket传输大数据文章中,我们可以顺利的接发数据,似乎做的不错,可以接收了 ...
- Java基础-synchronized关键字的用法(转载)
synchronized--同步 顾名思义是用于同步互斥的作用的. 这里精简的记一下它的使用方法以及意义: 当synchronized修饰 this或者非静态方法或者是一个实例的时候,所同步的锁是加在 ...
- Hbuider制作app升级包的简单办法 (升级官方提供的案例)
源文档:http://ask.dcloud.net.cn/question/11795 http://ask.dcloud.net.cn/article/199 一.生成移动App资源升级包 5+应用 ...
- [吴恩达机器学习笔记]13聚类K-means
13.聚类 觉得有用的话,欢迎一起讨论相互学习~Follow Me 13.1无监督学习简介 从监督学习到无监督学习 在一个典型的监督学习中,我们有一个有标签的训练集,我们的目标是找到能够区分正样本和负 ...
- libiconv的注意项
编译后有用的头文件zlib.h和zconf.h,使用时#include "zlib.h". 其中有三个核心的函数: iconv_ticonv_open(constchar*toco ...