Given 1->1->2->3->3, return 1->2->3.
定义一个pre 和cur
1.当二者不等的时候pre.next=cur pre=pre.next
2.当相等的时候pre.next=cur.next
3.每步都是cur=cur.next
代码如下:
# 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==None or head.next==None: return head dummy=ListNode(0) dummy.next=head pre=dummy.next cur=dummy.next while cur: if pre.val!=cur.val: pre.next=cur pre=pre.next else: pre.next=cur.next cur=cur.next return dummy.next
83. Remove Duplicates from Sorted List Leetcode Python
原文地址:http://blog.csdn.net/hyperbolechi/article/details/43503601