美文网首页
Remove Duplicates from Sorted Ar

Remove Duplicates from Sorted Ar

作者: 穿越那片海 | 来源:发表于2017-03-08 19:59 被阅读0次

Easy

这是有序数列去重的第二版本,序列为单向链接。这里需要用递归的方法解决。

  1. 比较序列的第一个节点与第二个节点,去除第一个节点处理余下序列,否则保留第一个节点处理余下序列
  1. 对余下序列重复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
        """
        try:
            if head.val != head.next.val:
                head.next = self.deleteDuplicates(head.next)
                return head
            else:
                head.next = self.deleteDuplicates(head.next)
                return head.next
        except:
            return head

相关文章

网友评论

      本文标题:Remove Duplicates from Sorted Ar

      本文链接:https://www.haomeiwen.com/subject/czxigttx.html