删除链表中重复节点(递归)

2024-01-06 09:39:18 浏览数 (1)

删除链表中重复节点(递归)

代码语言:javascript复制
public ListNode deleteDuplication(ListNode pHead){
    if(pHead == null || pHead.next == null)
        return pHead;
    ListNode current = pHead.next;
    
    // 如果是重复元素
    if(pHead.val == current.val){
        current = current.next;
        while(current != null && current.val == pHead.val)
            current = current.next;
        pHead = current;
        return deleteDuplication(current);
    }else{
        // pHead不是重复元素
        pHead.next = deleteDuplicaton(current);
        return pHead;
    }
}

0 人点赞