Remove Duplicats from Sorted List
题目:
Given a sorted linked list, delete all duplicates such that each element appear only once.
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
分析:
无
解法:
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if (head == null || head.next == null) {
return head;
}
ListNode curr = head;
while (curr.next != null) {
if (curr.val == curr.next.val) {
curr.next = curr.next.next;
} else {
curr = curr.next;
}
}
return head;
}
}