[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));
}

最新文章

  1. 在tmux中的vi 上下左右键变为了ABCD等字符
  2. a版本冲刺第二天
  3. JAVA学习心得
  4. hdu-5492 Find a path(dp)
  5. 超常用的PHP正则表达式收集整理
  6. zepto源码--classRE、maybeAddPx、children、defaultDisplay--学习笔记
  7. junit类找不到的问题解决
  8. 常用颜色的RGB值
  9. AllocateHwnd is not Thread-Safe
  10. Android设计模式系列--观察者模式
  11. 对Spring from中日期显示格式化问题
  12. 配置 .vimrc 解决 Vim / gVim 在中文 Windows 下的字符编码问题
  13. CListCtrl插入数据避免闪烁
  14. pwnable.kr leg之write up
  15. You Are the One DP
  16. java 线程池 ---- newFixedThreadPool()
  17. np.mat()和np.transpose
  18. 断网环境下利用pip安装Python离线安装包
  19. (转)注解用法详解—@@SuppressWarnings
  20. Linux下wc命令统计文件行数/词数/字符数/最长行字符数

热门文章

  1. Building simple plug-ins system for ASP.NET Core(转)
  2. 【计算机网络】NAT:网络地址转换
  3. 【bzoj4487】[Jsoi2015]染色问题 容斥原理
  4. 【bzoj1495】[NOI2006]网络收费 暴力+树形背包dp
  5. C++解析(2):进化后的 const 分析
  6. [BZOJ2432][Noi2011]兔农 矩阵乘法+exgcd
  7. Linux学习笔记二:Ubuntu安装SSH(Secure Shell)服务
  8. 解题:HNOI 2008 玩具装箱
  9. vue添加属性绑定
  10. Qt ------ 设置透明度