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
.
简单的题目,直接上AC代码:
ListNode* deleteDuplicates(ListNode* head) { if(!head) return NULL; ListNode*fir = head; ListNode*sec = fir->next; while(sec) { if(fir->val == sec->val) { sec= sec->next; } else { fir->next = sec; fir = sec; sec= sec->next; } } fir->next = sec; return head; }
时间: 2024-11-03 06:45:32