美文网首页leetcode题解
【Leetcode】83—Remove Duplicates f

【Leetcode】83—Remove Duplicates f

作者: Gaoyt__ | 来源:发表于2019-07-12 00:34 被阅读0次
    一、题目描述

    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
    示例:

    输入: 1->1->2
    输出: 1->2
    
    输入: 1->1->2->3->3
    输出: 1->2->3
    
    二、代码实现
    # 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 head is None: return head
            cur = head
            while cur.next:
                if cur.val == cur.next.val:
                    cur.next = cur.next.next
                else:
                    cur = cur.next
            return head
    

    相关文章

      网友评论

        本文标题:【Leetcode】83—Remove Duplicates f

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