83. Remove Duplicates from Sorted List(排序链表去重. 左右双指针)

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, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        fake = ListNode(-100000,head)
        pre = fake
        cur = head
        while cur:
            c_next = cur.next
            if pre.val == cur.val:
                pre.next = cur.next
                cur.next = None
            else:
                pre = cur
            cur =  c_next

        return fake.next

 

 

 

 

 
posted @ 2017-05-10 14:37  乐乐章  阅读(116)  评论(0编辑  收藏  举报