面试题:删除链表中的重复节点

题目描述:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

自己想测试用例  默默运行

测试用例主要是边界值,比如:空、全重复(11111)、全不重复(12345)

思路:

//如果全是1输出为空 执行过程
public class Solution {
    public ListNode deleteDuplication(ListNode pHead){
        ListNode first=new ListNode(0);
        first.next=pHead;
        ListNode last=first;
        while(pHead!=null&&pHead.next!=null){
            if(pHead.val==pHead.next.val){
                int val=pHead.val;
                //不加pHead!=null会报出空指针异常
                while(pHead!=null&&pHead.val==val){
                    pHead=pHead.next;
                }
                //last.next = p;的目的是跳出重复节点
                last.next=pHead;
            }else{
                //last=pHead;
                last.next=pHead;
                last=last.next;
                pHead=pHead.next;
            }
        }
        return first.next;
    }
}
原文地址:https://www.cnblogs.com/Aaron12/p/9537790.html