Remove duplicates from sorted list
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return []
firstPtr = head
secondPtr = head.next
while secondPtr is not None:
if secondPtr.val != firstPtr.val:
firstPtr.next = secondPtr
firstPtr = secondPtr
secondPtr = secondPtr.next
# Now point firstPtr.next to secondPtr (== None, at this point)
firstPtr.next = secondPtr
return headLast updated