[勇者闯LeetCode] 83. Remove Duplicates from Sorted List

来源:互联网 发布:算法设计与分析 沙特 编辑:程序博客网 时间:2024/06/04 18:35

[勇者闯LeetCode] 83. Remove Duplicates from Sorted List

Description

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.

Information

  • Tags: Linked List
  • Difficulty: Easy

Solution

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def deleteDuplicates(self, head):        """        :type head: ListNode        :rtype: ListNode        """        ans = head        while ans is not None and ans.next is not None:            if ans.next.val == ans.val:                ans.next = ans.next.next            else:                ans = ans.next        return head
0 0
原创粉丝点击