Data Structure and Algorithms - Sword Finger Offer Series (17) Delete Duplicate Nodes of Link List - Java Implementation

Posted by mpharo on Sat, 05 Oct 2019 04:56:00 +0200

Topic 17: Delete duplicate nodes in a linked list

Title Description:

How to delete duplicate nodes in a sorted list?

Ideas for solving problems:

  • The first step is to determine the parameters of the deletion function, which needs to input the header node of the list to be deleted. The header node may be duplicated with the latter node, that is to say, the header node may be deleted. Therefore, the deletion function declaration Delete Duplicated Node (ListNode pHead)
  • Next, traverse the entire list. If the value of the current node (pNode) is the same as the value of the next node, then it is a duplicate node. In order to ensure that the deleted code is continuous, the former node (pPreNode) of the current node should be connected to the node whose value is larger than that of the current node, so as to ensure that pPreNode is always connected to the next node without duplication.

Code implementation:

package swordToOffer;

import swordToOffer.Num17_DeleteListNode.ListNode;

public class Num18_DeleteDuplication {

	static class ListNode{
		int val;
		ListNode next;
		public ListNode(int value,ListNode nextNode) {
			val = value;
			next = nextNode;
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//Reread nodes are located at the head
		ListNode p4=new ListNode(3, null);
        ListNode p3=new ListNode(2, p4);
        ListNode p2=new ListNode(1, p3);
        ListNode p1=new ListNode(1, p2);
        //Head Node Repetition
        showListNode(p1);
        //Endpoint duplication
        p4=new ListNode(3,null);
        p3=new ListNode(3, p4);
        p2=new ListNode(2, p3);
        p1=new ListNode(1, p2);
        showListNode(p1);
        //Duplicate nodes are located in the middle
        p4=new ListNode(3,null);
        p3=new ListNode(2, p4);
        p2=new ListNode(2, p3);
        p1=new ListNode(1, p2);
        showListNode(p1);
        
        //Continuous occurrence of duplicate nodes
        ListNode p6 = new ListNode(5,null);
        ListNode p5 = new ListNode(5,p6);
        p4=new ListNode(3,p5);
        p3=new ListNode(3, p4);
        p2=new ListNode(1, p3);
        p1=new ListNode(1, p2);
        showListNode(p1);
        
        //Repeat multiple
        p6 = new ListNode(5,null);
        p5 = new ListNode(3,p6);
        p4=new ListNode(3,p5);
        p3=new ListNode(3, p4);
        p2=new ListNode(1, p3);
        p1=new ListNode(1, p2);
        showListNode(p1);
        
        //No repetition
        p6 = new ListNode(6,null);
        p5 = new ListNode(5,p6);
        p4=new ListNode(4,p5);
        p3=new ListNode(3, p4);
        p2=new ListNode(2, p3);
        p1=new ListNode(1, p2);
        showListNode(p1);
        
        //null node
        ListNode p7 = null;
        showListNode(p7);
	}
	
	public static ListNode deleteDuplicatiion(ListNode pHead) {
		if(pHead==null||pHead.next==null) {  //Empty nodes or only one
			return pHead;
		}
		ListNode preNode = null;
		ListNode curNode = pHead;
		
		while(curNode!=null) {
			boolean needDelete = false;
			if(curNode.next!=null&&curNode.val==curNode.next.val) {
				needDelete = true;
				//print
			    System.out.print("The node to be deleted is: ");
			    if(curNode!=null)
			        System.out.println(curNode.val);
			    else
			        System.out.println();
			}
			if(!needDelete) {   //Not duplicate nodes to find the next
				preNode = curNode;
				curNode = curNode.next;
			}else {
				int dupValue = curNode.val;
				ListNode toDeleted = curNode;
				while(toDeleted!=null&&toDeleted.val==dupValue) {
					//Find the next non-duplicate node
					toDeleted = toDeleted.next;
				}
				if(preNode==null) {
					pHead = toDeleted;
				}else {
					preNode.next = toDeleted;
				}
				
				curNode = toDeleted;  //Nodes cannot use next if they recur
			}
		}
		return pHead;
	}
	
	
	//	Display Function of 17 Questions
	public static void showListNode(ListNode head) {
	System.out.println("============");
    System.out.print("The original list is: ");
    ListNode curr=head;
    if(curr!=null) {
        while(curr.next!=null) {
            System.out.print(curr.val+",");
            curr=curr.next;
        }
        System.out.println(curr.val);
    }else {
        System.out.println();
    }
     
     
    curr=deleteDuplicatiion(head);    
    System.out.print("The result list is: ");
    if(curr!=null) {
        while(curr.next!=null) {
            System.out.print(curr.val+",");
            curr=curr.next;
        }
        System.out.println(curr.val);
    }else {
        System.out.println();
    }
    System.out.println("============");
}

}

 

Topics: P4