Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Alternative solution

Daven L

Oct 20, 2024

class Solution: def deleteDuplicates(self, head): # so I dont lose my original head, I set a variable current current = head # traverse through LL, ensuring curr and curr.next exist while current and current.next is not None: # prev becomes the value to which we compare other values to prev = current current = current.next # we move current until current.val no longer matches prev value # ensure we don't hit None by checking that current also exists while current and prev.val == current.val: current = current.next # whether current is now None or some number that is not prev.val, we set prev.next prev.next = current return head

This also seems like an O(n) solution.

0

0

Comments
Comments

On this page