forked from CodingVault/LeetCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_dups_from_sorted_list.py
59 lines (48 loc) · 1.47 KB
/
remove_dups_from_sorted_list.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
53
54
55
56
57
58
59
#!/usr/bin/env python
# encoding: utf-8
"""
remove_dups_from_sorted_list.py
Created by Shengwei on 2014-07-05.
"""
# https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
# tags: easy, linked-list, pointer, dups
"""
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.
"""
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
############# V2 #############
class Solution:
# @param head, a ListNode
# @return a ListNode
def deleteDuplicates(self, head):
cursor = head
while cursor and cursor.next:
if cursor.val == cursor.next.val:
cursor.next = cursor.next.next
else:
cursor = cursor.next
return head
############# V1 #############
class Solution:
# @param head, a ListNode
# @return a ListNode
def deleteDuplicates(self, head):
if head is None or head.next is None:
return head
walker, runner = head, head.next
while runner:
if walker.val != runner.val:
walker.next = runner
walker = runner
runner = runner.next
# IMPORTANT! truncate the linked list just in case
# test case: {1, 1} -> {1}
walker.next = None
return head