-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path82_RemoveDuplicatesFromSortedListII.py
52 lines (45 loc) · 1.36 KB
/
82_RemoveDuplicatesFromSortedListII.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
# @Last Modified time: 2016-04-29 17:15:38
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
# Recursively
class Solution(object):
def deleteDuplicates(self, head):
if not head or not head.next:
return head
if head.val == head.next.val:
while head.next and head.val == head.next.val:
head = head.next
return self.deleteDuplicates(head.next)
else:
head.next = self.deleteDuplicates(head.next)
return head
# Iteraively
class Solution_2(object):
def deleteDuplicates(self, head):
cur = pre_head = ListNode(0)
while head:
if head.next and head.val == head.next.val:
# Skip the duplicated nodes.
while head.next and head.val == head.next.val:
head = head.next
head = head.next
# we can make sure head is one single node here.
else:
cur.next = head
cur = cur.next
head = head.next
cur.next = None # Make sure the cur here is the tail: [1,2,2]
return pre_head.next
"""
[]
[1]
[1,2,2]
[3,3,3,3,3]
[1,1,1,2,3,4,4,4,4,5]
"""