leetcode-83. 删除排序链表中的重复元素

题目

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/

解法

class Solution {
    
    /**
     * @param ListNode $head
     * @return ListNode
     */
    function deleteDuplicates($head) {
        if (empty($head)) {
            return $head;
        }
        
        $preValue = null;
        /**
         * @var ListNode
         */
        $preNode = null;

        $dummyHead = $head;
        while ($dummyHead) {
            if ($preValue === $dummyHead->val) {
                $preNode->next = $dummyHead->next;
                $dummyHead = $dummyHead->next;
            } else {
                $preNode = $dummyHead;
                $dummyHead = $dummyHead->next;
                $preValue = $preNode->val;
            }
        }
        
        return $head;
    }
}
posted @ 2021-05-30 21:59  吴丹阳-V  阅读(25)  评论(0编辑  收藏  举报