时间: 2019-12-11
题目地址: https://leetcode.com/problems/remove-duplicates-from-sorted-list/
Given a sorted linked list, delete all duplicates such that each element appear only once.
给定一个排序的链表,删除所有重复项,使每个元素仅出现一次。
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
Solution:
public ListNode deleteDuplicates(ListNode head) {
if (null == head) {
return head;
}
ListNode point = head;
while (null != point.next) {
if (point.val == point.next.val) {
point.next = point.next.next;
} else {
point = point.next;
}
}
return head;
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Duplicates from Sorted List.
Memory Usage: 36 MB, less than 100.00% of Java online submissions for Remove Duplicates from Sorted List.