标签:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
问题:将有序链表中的重复元素删除
分析:由于有序,所以p结点是否重复只需要和它的前一节点比较是否相等即可
1 struct ListNode* deleteDuplicates(struct ListNode* head) { 2 struct ListNode* p, *r; 3 if(head == NULL || head->next == NULL) 4 { 5 return head; 6 } 7 p = head; 8 while(p->next) 9 { 10 if(p->val == p->next->val) 11 { 12 r = p->next; 13 p->next = p->next->next; 14 free(r); 15 } 16 else{ 17 p = p->next; 18 } 19 } 20 return head; 21 }
LeetCode[83]Remove Duplicates from Sorted List
标签:
原文地址:http://www.cnblogs.com/shiddong/p/4522688.html