# Simple iterative Python 6 lines, 60 ms

while cur:
while cur.next and cur.next.val == cur.val:
cur.next = cur.next.next     # skip duplicated node
cur = cur.next     # not duplicate of current node, move to next node

"""
:rtype: ListNode
"""
while cur and cur.next:
if cur.val == cur.next.val:
cur.next = cur.next.next
else:
cur = cur.next

• This only takes 56 ms, beats 75%

else:
prev = prev.next
return orig

• We don't need to judge whether cur is None in every step (while cur.next is enough). My solution is this:

"""
:rtype: ListNode
"""
while curr.next:
if curr.next.val == curr.val:
curr.next = curr.next.next
else:
curr = curr.next

• hey guys, i saw your code in three line"while cur:"
i know that i should check whether cur is none but i don't know why mine
'''
class Solution(object):
"""
:rtype: ListNode
"""
while fence.next:
if fence.next.val==last.val:
last.next=fence.next
del fence
fence=last
'''
it will mention me "Line 14: AttributeError: 'NoneType' object has no attribute 'next'"(line 14 is:fence=head.next)
can't fence just be None?

• @leonick
I think the reason is None is not a ListNode, thus it doesn't have attribute "next"

• @leonick When the input is an empty list '[]', 'head' will be 'None'. So 'head' will not have the attribute 'next'. That is the error occurred.

But when there is an empty head node in the front of the linked list, your code, I think, is right.

• 4 lines recursion solution