-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSolution83.java
More file actions
52 lines (48 loc) · 1.23 KB
/
Copy pathSolution83.java
File metadata and controls
52 lines (48 loc) · 1.23 KB
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
package leetcode.all.solution1_100;
import leetcode.common.LinkedUtils;
import leetcode.common.ListNode;
/**
* 83. 删除排序链表中的重复元素.
* 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
*
* 示例 1:
* 输入: 1->1->2
* 输出: 1->2
*
* 示例 2:
* 输入: 1->1->2->3->3
* 输出: 1->2->3
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class Solution83 {
/**
* 从第二个元素开始,和它之前的元素比较,
* 如果相同,删除当前节点,继续比较直至末尾
* @param head
* @return
*/
public ListNode deleteDuplicates(ListNode head) {
ListNode p=head,q=null;
while(p!=null){
if(q!=null&&p.val==q.val) {
q.next=p.next;
p=q.next;
}else{
q=p;
p=p.next;
}
}
return head;
}
public static void main(String[] args) {
int[] array={
1,1,2,2,2,2,2
};
ListNode head=LinkedUtils.arrayToLinkedList(array);
ListNode r=new Solution83().deleteDuplicates(head);
LinkedUtils.print(r);
}
}