删除排序链表中的重复元素

一、删除排序的链表中的重复元素

 题目描述:

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

 示例 1:

  输入: 1->1->2

  输出: 1->2

 示例 2:

  输入: 1->1->2->3->3

  输出: 1->2->3

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        
        
        pre, cur = head, head.next
        while cur:
            if cur.val == pre.val:
                cur = cur.next
                pre.next = cur
            else:
                pre = pre.next
                cur = cur.next
        return head

 

posted @ 2019-01-21 14:15  1直在路上1  阅读(221)  评论(0编辑  收藏  举报