Remove duplicates from Sorted List II
Given a sorted linked list, delete all nodes that has duplicate numbers, leaving only distinct numbers from the Original list.
For example,
Given1->2->3->3->4->4->5
, return1->2->5
.
Given1->1->1->2->3
, return2->3
.
Problem Solving Ideas:
The test instructions of this problem is to delete all elements in the ordered list that appear two or more times or more than two times. There is no greater difficulty in itself, note that the first element may be the case of repeating elements.
/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *}; */class Solution {public:listnode* deleteduplicates (listnode* head) {listnode* myhead=new ListNode (0); listnode* tail = myhead->next; listnode* pretail = Myhead; listnode* p = head, *q; while (P!=null) {if (tail==null) {tail = p; Pretail->next = tail; p=p->next; Tail->next = NULL; }else if (tail->val! = p->val) {tail->next = P; p=p->next; Tail = tail->next; Tail->next = NULL; Pretail = pretail->next; }else{while (p!=null && tail->val = = p->val) {q = p; p=p->next; Delete q; } delete tail; tail = NULL; pretail->next=null; }} P=myhead; Myhead = myhead->next; Delete p; return myhead; }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Leetcode] Remove duplicates from Sorted List II