82.删除排序链表中的重复元素II

【LeetCode刷题-链表】--82.删除排序链表中的重复元素II-LMLPHP

由于链表是排好序的,所以只需要对其进行一次遍历即可,比较相邻节点对应的值

/**
 * 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 dumyHead = new ListNode(0);
        dumyHead.next = head;
        ListNode cur = dumyHead;
        while(cur.next != null && cur.next.next != null){
            if(cur.next.val == cur.next.next.val){
                int x = cur.next.val;
                while(cur.next != null && cur.next.val == x){
                    cur.next = cur.next.next;
                }
            }else{
                cur = cur.next;
            }
        }
        return dumyHead.next;

    }
}
11-03 02:00