简繁EN

LeetCodeNo. 017

【LeetCode with Python】 83. Remove Duplicates from Sorted List

目錄

題目

原題頁面:https://leetcode.com/problems/remove-duplicates-from-sorted-list/
題目型別:Linked List
難度評價:Easy

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.


分析

有序連結串列的去重,注意及時檢查一些引用是否為None。


程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
# @param head, a ListNode
# @return a ListNode
def deleteDuplicates(self, head):
if None == head or None == head.next:
return head

cur = head
while None != cur:
if None != cur.next and cur.val == cur.next.val:
cur.next = cur.next.next
continue
else:
cur = cur.next

return head

本文地址:https://xnerv.wang/zh-tw/leetcode-remove-duplicates-from-sorted-list/