1 题目
2 Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode ans = head;
ListNode front = head;
ListNode tail;
while (front != null && front.next != null) {
// 如果front的值与tail的值相等
tail = front.next;
if (front.val == tail.val) {
front.next = tail.next;
tail.next = null;
} else {
front = tail;
}
}
return ans;
}
}
2 关键点
一前以后指针