summary:
linked list
package myapp.kit.leetcode.top200;
import myapp.kit.leetcode.base.ListNode;
/**
* 83
* easy
*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
*
* @author huangdingsheng
* @version 1.0, 2020/7/9
*/
public class remove_duplicates_from_sorted_list_83 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode n1 = head;
ListNode n2 = head.next;
while (n2 != null) {
if (n1.val == n2.val) {
n1.next = n2.next;
n2 = n2.next;
} else {
n1 = n1.next;
n2 = n2.next;
}
}
return head;
}
}