:type head: ListNode :rtype: ListNode
(self, head)
| 31 | |
| 32 | class Solution(object): |
| 33 | def deleteDuplicates(self, head): |
| 34 | """ |
| 35 | :type head: ListNode |
| 36 | :rtype: ListNode |
| 37 | """ |
| 38 | if not head: |
| 39 | return head |
| 40 | |
| 41 | x = head |
| 42 | |
| 43 | while head.next: |
| 44 | if head.val == head.next.val: |
| 45 | head.next = head.next.next |
| 46 | head.duplicate = True |
| 47 | else: |
| 48 | head = head.next |
| 49 | |
| 50 | while x: |
| 51 | if hasattr(x, 'duplicate'): |
| 52 | x = x.next |
| 53 | else: |
| 54 | break |
| 55 | |
| 56 | if not x: |
| 57 | return None |
| 58 | |
| 59 | new = ListNode(x.val) |
| 60 | x = x.next |
| 61 | _new = new |
| 62 | |
| 63 | while x: |
| 64 | if hasattr(x, 'duplicate'): |
| 65 | x = x.next |
| 66 | else: |
| 67 | new.next = ListNode(x.val) |
| 68 | new = new.next |
| 69 | x = x.next |
| 70 | return _new |