We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My Java solution with o(n) time complexity and o(1) space complexity:
publicstaticSinglyLinkedListNoderemoveDuplicates(SinglyLinkedListNodellist){if(llist==null)returnllist;//no duplicates to rmv//use two ptrs to iterate over the linked listSinglyLinkedListNodefirst=llist;SinglyLinkedListNodesecond=llist.next;while(first!=null&&second!=null){//remove the second ptr if its data is equal to the first ptrif(first.data==second.data)first.next=second.next;//otherwise increase first ptr since data isnt equalelsefirst=second;//increment second ptr to go through entire listsecond=second.next;}returnllist;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Delete duplicate-value nodes from a sorted linked list
You are viewing a single comment's thread. Return to all comments →
My Java solution with o(n) time complexity and o(1) space complexity: