刪除鏈表中重復的結點(劍指offer)
阿新 • • 發佈:2019-04-16
let 怎麽辦 itl style amp bject 劍指offer 題目 des
題目描述
在一個排序的鏈表中,存在重復的結點,請刪除該鏈表中重復的結點,重復的結點不保留,返回鏈表頭指針。 例如,鏈表1->2->3->3->4->4->5 處理後為 1->2->5 方法一:遞歸 兩種情況,如果是重復結點怎麽辦?遇到了就跳過,返回重復節點的下一個結點。 遇到不重復結點?遇到不重復結點直接連上。/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode deleteDuplication(ListNode pHead) { if(pHead==null||pHead.next==null) return pHead; if(pHead.val==pHead.next.val){ ListNode pNode=pHead; while (pNode!=null && pNode.val==pHead.val){ pNode=pNode.next; } return deleteDuplication(pNode); }else { pHead.next=deleteDuplication(pHead.next); return pHead; } } }
刪除鏈表中重復的結點(劍指offer)