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

topic

In a sorted linked list, there are duplicate nodes, please delete the duplicate nodes in the linked list, the duplicate nodes are not retained, and the pointer to the head of the linked list is returned. For example, the linked list 1->2->3->3->4->4->5 is processed as 1->2->5

Thought analysis

Idea 1: Use LinkedList to save unique nodes and reconstruct the linked list

  • Use two scan pointers one after the other (pwork = pHead; pafter = pHead.next;)
  • The front and back are not equal to the pwork entry, otherwise the double pointer continues to move backward.
  • A flag needs to be used to mark whether the two nodes that are currently unequal are the previous duplicate nodes. Therefore, when the front and back are not equal, after judging whether to join the team, move the pointer and process the flag accordingly.
  • At the end of the special judgment, at this time, pwork.next == null cannot be compared before and after, and it is necessary to determine whether the current pwork is the previous repeated node or the new node through the flag.

Analysis and evaluation: This method is a relatively straightforward and easy-to-think method. As long as you pay attention to the distinction and judgment of some situations when using it, you should be able to AC quickly.


Idea 2: Completely modify the original linked list through the "pointer" operation, without using additional storage linked list space

  • scan pointer (pwork = pHead; )
  • Precursor pointer (ppre = pHead;)
  • Scan the linked list, the front and back nodes are not equal ==> move all pointers; the front and back nodes are equal, move pwork backwards until the inequality is encountered again ==> ppre directly connects pwork.next over the repeated nodes in the middle;
  • Special judgment at the end: the same until the end, ppre.next = null;

Evaluation: This method is better than method 1 in space, and the time complexity is O(n)


code show as below:

public class DeleteDuplication {
    public ListNode deleteDuplication(ListNode pHead) {
        //special judge
        if (pHead == null || pHead.next == null)
            return pHead;

        ListNode ppre = new ListNode(-1);
        ppre.next = pHead;
        ListNode pwork = pHead;

        while (pwork != null && pwork.next != null) {
            if(pwork.val == pwork.next.val){
                do{
                    pwork = pwork.next;
                    if(pwork.next == null) {
                        ppre.next = null;
                        break;
                    }
                }while(pwork.val == pwork.next.val);
                if(pwork.val == pHead.val){
                    pHead = pwork.next;
                    ppre.next = pHead;
                    pwork = pwork.next;
                }else{
                    ppre.next = pwork.next;
                    pwork = pwork.next;
                }
            }else{
                ppre = ppre.next;
                pwork = pwork.next;
            }
        }

        return pHead;
    }
}

Summarize

  • Traversal condition: The traversal pointer is not null (pwork != null)
  • Before and after comparison conditions: the latter pointer is not null (pwork.next != null && pwork != null)
  • When the linked list pointer moves, it is best to follow the "termination judgment"

Guess you like

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