while node: if node.val != sentinel.val: sentinel.next = node sentinel = node node = node.next if sentinel: sentinel.next = None return head def deleteDuplicates2(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return head node = head while node.next: if node.val == node.next.val: node.next = node.next.next else: node = node.next return head solution = Solution() head = linkutils.parse_array_2_list([1, 2, 3, 3, 3]) solution.deleteDuplicates2(head) linkutils.iterate_linked_list(head)
:param head: ListNode :param m: int :param n: int :return: ListNode """ before = dummy = linkutils.ListNode(-1) dummy.next = head for i in range(m - 1): if before is None: return head before = before.next current = start = before.next if start is None: return head before.next = None j = 0 for i in range(n - m + 1): if current is None: break next = current.next current.next = before.next before.next = current current = next start.next = current return dummy.next head = reverse_list(linkutils.parse_array_2_list([0, 1, 2, 3, 4]), 3, 4) linkutils.iterate_linked_list(head)