LeetCode 83 [Remove Duplicates from Sorted List]

原题

给定一个排序链表,删除所有重复的元素每个元素只留下一个。

样例
给出 1->1->2->null,返回 1->2->null
给出 1->1->2->3->3->null,返回 1->2->3->null

解题思路

  • 基础链表操作,遍历一遍去重

完整代码

# 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
        """
        res = head
        if not head:
            return res
        while head.next != None:
            if head.val == head.next.val:
                head.next = head.next.next
            else:
                head = head.next
        return res

你可能感兴趣的:(LeetCode 83 [Remove Duplicates from Sorted List])