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;
}
}
本文来自博客园,作者:吴丹阳-V,转载请注明原文链接:https://www.cnblogs.com/wudanyang/p/14829319.html