标签:style io ar color os sp for on bs
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); }
Remove Duplicates from Sorted List
标签:style io ar color os sp for on bs
原文地址:http://blog.csdn.net/uj_mosquito/article/details/41518847