admin 管理员组

文章数量: 1087749

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

[勇者闯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 = headwhile ans is not None and ans.next is not None:if ans.next.val == ans.val:ans.next = ans.next.nextelse:ans = ans.nextreturn head

本文标签: 勇者闯LeetCode 83 Remove Duplicates from Sorted List