[勇者闯LeetCode] 83. Remove Duplicates from Sorted List
Description
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given1->1->2
, return1->2
.
Given1->1->2->3->3
, return1->2->3
.
Information
- Tags: Linked List
- Difficulty: Easy
Solution
# 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
"""
ans = head
while ans is not None and ans.next is not None:
if ans.next.val == ans.val:
ans.next = ans.next.next
else:
ans = ans.next
return head