Leetcode: Remove duplicate elements in the sorted list (Java)

Delete duplicate elements in the linked list

Title description:

There is a linked list arranged in ascending order. Given the head node of this linked list  head , please delete all duplicate elements so that each element  only appears once  .

Return a linked list of results also in ascending order.

Example:

Input: [1,2,2,3,3]

Output: [1,2,3]

Ideas:

The linked list is a sorted linked list, so the position of repeated elements in the linked list is also continuous . To delete duplicate elements in the linked list, only need to traverse the linked list .

  1. Let current point to the head node of the linked list, and current traverses from the head node of the linked list;
  2. If the value of current is equal to the value of current.next, delete current.next; (let current.next point to current.next.next)
  3. Otherwise, let current go all the way backwards until current.next == null

Code:

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        //不会删除头节点
        ListNode current = head;
        while(current.next!= null){
            if(current.val == current.next.val){
                current.next = current.next.next;
            }else{
                current = current.next;
            }
        }
        return head;
    }
}

Delete duplicate elements in the linked list II

Title description:

There is a linked list arranged in ascending order. Give you the head node head of this linked list. Please delete all nodes in the linked list that have repeated numbers , and only keep the numbers that are not repeated in the original linked list.

Return a linked list of results in ascending order

Example:

Input: [1,2,3,3,4,4,5]

Output: [1,2,5]

Ideas:

Like the above, duplicate nodes are also deleted through traversal.

Since all duplicate elements are to be deleted, the head node of the linked list may be deleted, so use the dummy node to point to the head node of the linked list.

  1. Let prev point to the puppet node, and current point to the head node of the linked list;
  2. When the value of current is equal to the value of current.next, let current point to current.next
  3. If they are not equal, judge whether there is a node between prev and current, go backward without prev, otherwise, prev.next points to current.next (skip the repeated elements)
  4. As long as current is not null, let current go all the way back.

Code:

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
       //删除排序链表中的重复元素
        if(head == null|| head.next == null){
            return head;
        }
        //可能会删除head节点,所有用虚拟节点
        ListNode newHead = new ListNode(-1);
        newHead.next = head;

        ListNode prev = newHead;

        ListNode current = head;
        while (current != null){
            while (current.next != null && current.val == current.next.val){
                current = current.next;
            }
            if (prev.next == current){
                //prev和current之间没有重复的节点,prev向后移动
                prev = prev.next;
            }else {
                //跳过了重复的元素
                prev.next= current.next;
            }
            current = current.next;
        }
        return newHead.next;
    }
}

 

 

Guess you like

Origin blog.csdn.net/qq_45754055/article/details/115224755