The sword refers to offer - delete duplicate nodes in the linked list

delete duplicate nodes in linked list

Two methods:

Law 1: Not very good

With the help of HashMap, each node is added to the map, the key is the node value, and the value is a boolean type. If it is true, it means that it has been added once, and false means it has been added twice or more.

Before joining, judge whether the value of the node already exists in the map,

If it doesn't exist, join(node.val, true)

If it exists, join(node.val, false)

Then traverse the linked list again, and find the key value in the map at the same time, if it is true, put it into the new node, otherwise ignore it

public class Solution {
    public ListNode deleteDuplication(ListNode pHead){
        if (pHead == null || pHead.next == null) return pHead;
        HashMap<Integer, Boolean> map = new HashMap<>();
        ListNode node = pHead;
        while(node != null){
            if(map.containsKey(node.val)){
                map.put(node.val, false);
            }else{
                map.put(node.val, true);
            }
            node = node.next;
        }
        for(HashMap.Entry<Integer, Boolean> entry : map.entrySet()){
        }
        ListNode head = new ListNode(0);
        node = head;
        while(pHead != null){
            if(map.get(pHead.val)){
                node.next = pHead;
                node = node.next;
            }
            pHead = pHead.next;
        }
        node.next = null;
        node = head;
        while(node != null){
            node = node.next;
        }
        return head.next;
    }
}

  

 

Method 2: use

newHead, (the new head node, used to return)

newNext (used to connect the nodes behind the new node),

First point both nodes to the new new ListNode(0),

When traversing the linked list, judge the value of the current node and the next node of the linked list,

If it is equal, record the value val of the current node, and start traversing. When you know that the value of the current node is not val, come out and continue to judge (do not record the current node, because all duplicate nodes will be deleted)

If not equal, newNext = the value of the current node

The last point to note is that when the last node is not repeated, it is impossible to judge, so newNext = pHead (that is, the last node is repeated, then in the above loop, it has been traversed, now pHead points to null)

public class Solution {
    public ListNode deleteDuplication(ListNode pHead){
        if (pHead == null || pHead.next == null) return pHead;
        ListNode newHead = new ListNode(0);
        ListNode newNext = newHead;
        while(pHead != null && pHead.next != null){
            if (pHead.val == pHead.next.val)
                int val = pHead.val;
                while(pHead != null && pHead.val == val){
                    pHead = pHead.next;
                }
            }else{
                newNext.next = pHead;
                newNext = newNext.next;
                pHead = pHead.next;
            }
        }

        newNext.next = pHead;
        return newHead.next;
    }
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325169815&siteId=291194637