Given a sorted linked list, delete all duplicates such that each element appear onlyonce.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
思路: 去重复,cur, node即可;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummpy = new ListNode(-1);
dummpy.next = head;
ListNode cur = head;
while(cur != null) {
ListNode curnext = cur.next;
if(curnext != null && cur.val == curnext.val) {
cur.next = curnext.next;
} else {
cur = curnext;
}
}
return dummpy.next;
}
}