![]()
1 import java.util.*;
2
3 /*
4 * public class ListNode {
5 * int val;
6 * ListNode next = null;
7 * public ListNode(int val) {
8 * this.val = val;
9 * }
10 * }
11 */
12 public class Solution {
13 /**
14 * @param head ListNode类
15 * @return ListNode类
16 */
17 public ListNode deleteDuplicates (ListNode head) {
18 // 判空链表
19 if(head == null)
20 return null;
21 // 申请临时指针
22 ListNode current = head;
23 // 指针当前和下一位不为空
24 while(current != null && current.next != null){
25 // 如果当前与下一位相等则忽略下一位
26 if(current.val == current.next.val)
27 current.next = current.next.next;
28 // 否则指针正常遍历
29 else
30 current = current.next;
31 }
32 // 返回结果
33 return head;
34 }
35 }