[002] delete_duplication_of_linked_list
2024-09-18 16:44:58
[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));
}
最新文章
- 在tmux中的vi 上下左右键变为了ABCD等字符
- a版本冲刺第二天
- JAVA学习心得
- hdu-5492 Find a path(dp)
- 超常用的PHP正则表达式收集整理
- zepto源码--classRE、maybeAddPx、children、defaultDisplay--学习笔记
- junit类找不到的问题解决
- 常用颜色的RGB值
- AllocateHwnd is not Thread-Safe
- Android设计模式系列--观察者模式
- 对Spring from中日期显示格式化问题
- 配置 .vimrc 解决 Vim / gVim 在中文 Windows 下的字符编码问题
- CListCtrl插入数据避免闪烁
- pwnable.kr leg之write up
- You Are the One DP
- java 线程池 ---- newFixedThreadPool()
- np.mat()和np.transpose
- 断网环境下利用pip安装Python离线安装包
- (转)注解用法详解—@@SuppressWarnings
- Linux下wc命令统计文件行数/词数/字符数/最长行字符数
热门文章
- Building simple plug-ins system for ASP.NET Core(转)
- 【计算机网络】NAT:网络地址转换
- 【bzoj4487】[Jsoi2015]染色问题 容斥原理
- 【bzoj1495】[NOI2006]网络收费 暴力+树形背包dp
- C++解析(2):进化后的 const 分析
- [BZOJ2432][Noi2011]兔农 矩阵乘法+exgcd
- Linux学习笔记二:Ubuntu安装SSH(Secure Shell)服务
- 解题:HNOI 2008 玩具装箱
- vue添加属性绑定
- Qt ------ 设置透明度