More linked lists problem solution | 30 days of code | HackerRank

Input Format
You do not need to read any input from stdin. The following input is handled by the locked stub code and passed to the removeDuplicates function:
The first line contains an integer, , the number of nodes to be inserted.
The  subsequent lines each contain an integer describing the  value of a node being inserted at the list's tail.
Constraints
  • The data elements of the linked list argument will always be in non-decreasing order.
Output Format
Your removeDuplicates function should return the head of the updated linked list. The locked stub code in your editor will print the returned list to stdout.
More linked lists problem | solution 30 days of code | HackerRank

Problem solution in Java programming language.

public static Node removeDuplicates(Node head) {
    // Empty list case:
    if (head == null) {
        return head;
    }

    Node current = head;
    while (current.next != null) {
        // If data is equal, reset the next pointer
        if (current.data == (current.next).data) {
            current.next = (current.next).next;
        } else {
            // If data is not equal, go
            // to the next element
            current = current.next;
        }
    }

    return head;
}

Second solution



public static Node removeDuplicates(Node head) {
    // if head is null or list only has 1 element
    if( head == null || head.next == null){
        return head;
    }

    // if list is more than 2 elements
    Node current = head.next;
    Node previous = head;
    while( current != null ){
        // If current is a duplicate of previous
        if( previous.data == current.data ){
            // set previous' next pointer to skip over current node
            previous.next = current.next;
            // set current to next node in list, previous should not move
            current = current.next;
        }
        else{ // node is not duplicate of previous
            previous = current;
            current = current.next;
        }
    }
    return head;
}

Third solution

public static Node removeDuplicates(Node head) {
    // if head is null or list only has 1 element
    if( head == null || head.next == null){
        return head;
    }

    // if list is more than 2 elements
    Node current = head;
    while( current.next != null ){
        // If current is a duplicate of previous
        if( current.data == current.next.data ){
            // set previous' next pointer to skip over current node
            current.next = current.next.next;
        }
        else{ // node is not duplicate of previous
            current = current.next;
        }
    }
    return head;
}

Problem solution in Python programming language.



class Node:
    def __init__(self,data):
        self.data = data
        self.next = None 
class Solution: 
    def insert(self,head,data):
            p = Node(data)           
            if head==None:
                head=p
            elif head.next==None:
                head.next=p
            else:
                start=head
                while(start.next!=None):
                    start=start.next
                start.next=p
            return head  
    def display(self,head):
        current = head
        while current:
            print(current.data,end=' ')
            current = current.next

    def removeDuplicates(self,head):
        qu = []
        if head == None:
            return 
        p = head
        qu.append(p.data)
        p = p.next
        while p is not None:
            qu.append(p.data)
            p = p.next
        qu = list(dict.fromkeys(qu))
        for item in qu:
            print(item,end=' ')

mylistSolution()
T=int(input())
head=None
for i in range(T):
    data=int(input())
    head=mylist.insert(head,data)    
head=mylist.removeDuplicates(head)
mylist.display(head); 

Post a Comment

0 Comments