Today we will see the HackerRank Day 24 Solution in Python. The problem is named More Linked Lists which is part of 30 Days of code on HackerRank. Let’s get started!
Day 24: More Linked Lists Problem statement
We are given a Node class and a Node object that has an integer data field, and a Node instance pointer, pointing to another node (i.e.: the next node in a list). A removeDuplicates function is declared which takes a pointer to the node of a linked list as a parameter. We have to Complete removeDuplicates so that it deletes any duplicate nodes from the list and returns the head of the updated list.
Sample Input
6
1
2
2
3
3
4
Sample Output
1 2 3 4
Explanation: First input is the number of elements followed by the elements to be inserted in list. The values 2 and 3 both occur twice in the list, so we remove the two duplicate nodes. We then return our updated (ascending) list
You can solve the problem here.
HackerRank Day 24 Solution in Python
class Node: def __init__(self,data): self.data = data self.next = None class Solution: #Function to insert the new node with data 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 #Function to display the list def display(self,head): current = head while current: print(current.data,end=' ') current = current.next #Function to remove duplicates def removeDuplicates(self,head): if not head: return head new_node = head #Iterate through the list while new_node.next: #If the current node and next node data are same if new_node.data == new_node.next.data: new_node.next = new_node.next.next else: new_node = new_node.next #Return head of the updated list return head mylist= Solution() 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);
Code Explanation
- Create a function to remove duplicates in the list
- If the head is null, return the head. Else store the head in a variable
- Iterate through the list, and check if the current node and next node data are the same
- If it is the same delete the current element, else move to the next element
- After reaching the end of the list, return the head value of this updated list without duplicates
Also Read:
- HackerRank Day 8 Solution in Python: Dictionaries and Maps
- HackerRank Day 7 Solution in Python: Arrays
- HackerRank Day 6 Solution in Python: Let’s review
- HackerRank Day 5 Solution in Python: Loops
- HackerRank Day 4 Solution in Python: Class vs Instance
- HackerRank Day 3 Solution in Python: Intro to Conditional Statements
- HackerRank Day 2 Solution in Python: Operators
- HackerRank Day 1 Solution in Python: Data Types
- HackerRank Day 0 Solution in Python: Hello World
- HackerRank Day 29 Solution in Python: Bitwise AND
- HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases
- HackerRank Day 27 Solution in Python: Testing
- HackerRank Day 26 Solution in Python: Nested Logic
- HackerRank Day 25 Solution in Python: Running Time and Complexity
- HackerRank Day 24 Solution in Python: More Linked Lists
- HackerRank Day 23 Solution in Python: BST Level Order Traversal
- HackerRank Day 22 Solution in Python: Binary Search Trees
- HackerRank Day 20 Solution in Python: Sorting
- HackerRank Day 19 Solution in Python: Interfaces
- HackerRank Day 18 Solution in Python: Queues and Stacks
- HackerRank Day 17 Solution in Python: More Exceptions
- HackerRank Day 16 Solution: Exceptions – String to Integer
- HackerRank Day 15 Solution in Python: Linked List
- HackerRank Day 13 Solution in Python: Abstract Classes
- HackerRank Day 14 Solution in Python: Scope
- HackerRank Day 12 Solution in Python: Inheritance
- HackerRank Day 11 Solution in Python: 2D Arrays
- HackerRank Day 10 Solution in Python: Binary Numbers