昨天身體實在不舒服,又間斷了一天 ??
好,今天繼續(xù)鏈表題目
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/
刪除一個排序列表里的重復(fù)元素
這道題,恰恰是因為排序后,重復(fù)元素一定是“相鄰”的,所以事情就簡單咯
刪除鏈表節(jié)點(diǎn)的方法還是知道該節(jié)點(diǎn)的前一個節(jié)點(diǎn),然后將他前一個節(jié)點(diǎn)的next指向這個節(jié)點(diǎn)的next,這句話確實有點(diǎn)“繞”。
但道理就是這么個道理。
# 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 head == None:
return head
cur = head
while cur.next != None:
if cur.val == cur.next.val:
cur.next = cur.next.next
else:
cur = cur.next
return head