Given a sorted linked list, delete all nodes that has duplicate numbers, leaving only distinct numbers from the Original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
Hide TagsLinked List It's okay to control it.
#include <iostream>using namespacestd;/** * Definition for singly-linked list.*/structListNode {intVal; ListNode*Next; ListNode (intx): Val (x), Next (NULL) {}};classSolution { Public: ListNode*deleteduplicates (ListNode *head) {ListNode ret (0); ListNode* pslow=&ret,*pfast=Head; while(1){ if(Pfast==null) Break; BOOLDUP =false; intCurval = pfast->Val; Pfast=pfast->Next; while(pfast!=null&&pfast->val==curval) {DUP=true; Pfast=pfast->Next; } if(dup==false) {Pslow->next =NewListNode (Curval); Pslow=pslow->Next; } } returnRet.next; }};intMain () {return 0;}
[Leetcode] Remove duplicates from Sorted list II linked list