Skip to content

Latest commit

 

History

History
61 lines (55 loc) · 1.34 KB

remove-duplicates-from-sorted-list.md

File metadata and controls

61 lines (55 loc) · 1.34 KB

Remove Duplicates from Sorted List

--

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param head, a ListNode
    # @return a ListNode
    def deleteDuplicates(self, head):
        if head is None or head.next is None:
            return head
        cur = head
        ptr = head.next
        while ptr is not None:
            while ptr != None and cur.val == ptr.val:
                ptr = ptr.next
            cur.next = ptr
            cur = ptr
        return head

--

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param head, a ListNode
    # @return a ListNode
    def deleteDuplicates(self, head):
        if head is None or head.next is None:
            return head
        nhead = ListNode(0)
        nhead.next = head
        pre = nhead
        ptr = head
        while ptr is not None and ptr.next is not None:
            uniq = True
            while ptr.next != None and ptr.val == ptr.next.val:
                uniq = False
                ptr = ptr.next
            if uniq:
                pre.next = ptr
                pre = ptr
            ptr = ptr.next
        pre.next = ptr
        return nhead.next