Remove Duplicates from Sorted List
来源:互联网 发布:excel快速录入数据 编辑:程序博客网 时间:2024/06/07 19:35
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
#include<stdio.h> #include<stdlib.h> typedef struct ListNode { int val; struct ListNode *next; }ListNode; ListNode *deleteDuplicates(ListNode *head) { ListNode *p1=head,*p2=head; if(head==NULL) return head; while(p1->next!=NULL && p2!=NULL){ if(p1->next->val==p1->val) { p2->next=p2->next->next; p1=p2; } else { p2=p1; p1=p1->next; } } return head;}void main(){ ListNode *p1,*p2,*head; int i; p1=(ListNode*)malloc(sizeof(ListNode)); scanf("%d",&p1->val); for(i=1;p1->val!=0;i++){ if(i==1) head=p1; else p2->next=p1; p2=p1; p1=(ListNode*)malloc(sizeof(ListNode)); scanf("%d",&p1->val); } p2->next=NULL; head=deleteDuplicates(head); for(p1=head;p1!=NULL;p1=p1->next) printf("%d\n",p1->val);}
0 0
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove duplicates from sorted list
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- android framework层 学习笔记(一)
- 用Python写一个 Hadoop MapReduce 程序
- 我是如何在SQLServer中处理每天四亿三千万记录的
- Length of Last Word (leetcode)
- 百度地图功能封装
- Remove Duplicates from Sorted List
- Asp.net Mvc 中的模型绑定
- JavaScript——日期时间
- RelativeLayout和ImageView 使用体验
- 密码发生器
- 今年回家不花冤枉钱,而且不拥挤
- 巡线小车源程序(MSP430,PID)
- 轻松一下行不行
- SQL Server将一列的多行内容拼接成一行