Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2 Output: 1->2
Example 2:
Input: 1->1->2->3->3 Output: 1->2->3
Code:
class Solution:
def deleteDuplicates(self, head):
res = []
while head != None:
con = head.val
if con not in res:
res.append(con)
head = head.next
if res = []:
return None
out = ListNode(res[0])
num = len(res)
if num == 1:
return out
else:
rear = out
for x in range(1, num):
mid = ListNode(res[x])
rear.next = mid
rear = rear.next
return out