题目
https://leetcode.com/problems/remove-duplicates-from-sorted-list/
解题
还是递归。
注意两点:
1,用递归前要判别head和head.next是否为空,;
2,最后要判别第一第二个值是否相等。
# 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 or not head.next:
return head
head.next = self.deleteDuplicates(head.next)
if head.val == head.next.val:
return head.next
else:
return head