删除链表中重复的结点
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&&tqId=11209&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
public ListNode deleteDuplication(ListNode pHead) { if(pHead == null || pHead.next == null) return pHead; ListNode newNode = new ListNode(0); //新建结点,防止头节点被删除 newNode.next = pHead; ListNode tmp = pHead; ListNode pre = newNode; //保存前一个结点 while(tmp != null && tmp.next != null){ if(tmp.val == tmp.next.val){ int val = tmp.val; //将跟tmp结点值相等的结点过滤掉 while(tmp != null && tmp.val == val){ tmp = tmp.next; } //上一个非重复的结点指向下一个非重复的结点 pre.next = tmp; }else{ pre = tmp; tmp = tmp.next; } } return newNode.next; }
剑指offer 文章被收录于专栏
为刷过的每一道题都书写一篇题解,便于重复练习~