Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
思路:没啥难度,注意空链表和长度只有1的链表即可.
# 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 head
if not head.next:
return head
tmp = head
while tmp.next:
if tmp.val == tmp.next.val:
tmp.next = tmp.next.next
else:
tmp = tmp.next
return head