public ListNode deleteDuplication(ListNode pHead) { if(pHead==null){ return null; } ListNode preNode = null; ListNode curNode = pHead; while(curNode != null){ ListNode nextNode=curNode.next; boolean needDeleted = false; if(nextNode!=null && curNode.val == nextNode.val){ needDeleted=true; } if(!needDeleted){ preNode=curNode; curNode=curNode.next; }else { int value=curNode.val; ListNode toBeDeleted = curNode; while(toBeDeleted!=null && toBeDeleted.val == value){ nextNode=toBeDeleted.next; toBeDeleted.next=null; toBeDeleted=nextNode; } curNode=toBeDeleted; if(preNode==null){ pHead= curNode; }else{ preNode.next=curNode; } } } return pHead; }
原文地址:https://www.cnblogs.com/Allen-win/p/8324645.html
时间: 2024-11-05 21:44:42