-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path83.删除排序链表中的重复元素.php
88 lines (83 loc) · 1.58 KB
/
83.删除排序链表中的重复元素.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*
* @lc app=leetcode.cn id=83 lang=php
*
* [83] 删除排序链表中的重复元素
*
* https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/
*
* algorithms
* Easy (53.69%)
* Likes: 573
* Dislikes: 0
* Total Accepted: 255K
* Total Submissions: 475K
* Testcase Example: '[1,1,2]'
*
* 存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次 。
*
* 返回同样按升序排列的结果链表。
*
*
*
* 示例 1:
*
*
* 输入:head = [1,1,2]
* 输出:[1,2]
*
*
* 示例 2:
*
*
* 输入:head = [1,1,2,3,3]
* 输出:[1,2,3]
*
*
*
*
* 提示:
*
*
* 链表中节点数目在范围 [0, 300] 内
* -100
* 题目数据保证链表已经按升序排列
*
*
*/
// @lc code=start
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution
{
/**
* @param ListNode $head
* @return ListNode
*/
public function deleteDuplicates($head)
{
$p = $head;
while ($p->next) {
$pt = $p->next;
while ($pt) {
if ($p->val === $pt->val) {
// 删除 pt
$p->next = $pt->next;
}
$pt = $pt->next;
}
$p = $p->next;
}
return $head;
}
}
// @lc code=end