Topic:
The first time I brushed the problem, I missed it.
Given a sorted linked list, delete all duplicates such this each element appear only once.
For example,
Given 1->1->2 , return 1->2 .
Given 1->1->2->3->3 , return 1->2->3 .
Code:
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode* Deleteduplicates (listnode*head) { if(!head)returnHead; ListNode Dummpy (-1); Dummpy.next=Head; ListNode* Pre =Head; ListNode* Curr = head->Next; while(curr) {if(curr->val!=pre->val) {Pre=Curr; Curr= curr->Next; } Else{Pre->next = curr->Next; Curr= curr->Next; } } returnDummpy.next; }};
Tips
Tangled up a bit before AC. The reason is that the first time you write:
Pre = Curr;
Curr = curr->next;
"Remove duplicates from Sorted List" CPP