while current: k = current.val hash_counter[k] = hash_counter.get(k, 0) + 1 current = current.next new_head = None prev, current = None, head while current: if hash_counter[current.val] >= 2: if prev: prev.next = current.next prev, current = prev, current.next else: if not new_head: new_head = current prev, current = current, current.next return new_head if __name__ == '__main__': ListNode.print_linked_list(Solution().deleteDuplicates( ListNode.build_linked_list([1, 1, 2, 3, 1, 1, 2]))) ListNode.print_linked_list(Solution().deleteDuplicates( ListNode.build_linked_list([1, 1, 2, 3, 1, 1]))) ListNode.print_linked_list(Solution().deleteDuplicates( ListNode.build_linked_list([1, 2, 3, 3, 4, 4, 5]))) ListNode.print_linked_list(Solution().deleteDuplicates( ListNode.build_linked_list([1, 5]))) ListNode.print_linked_list(Solution().deleteDuplicates( ListNode.build_linked_list([1]))) ListNode.print_linked_list(Solution().deleteDuplicates( ListNode.build_linked_list([])))
:rtype: ListNode """ if not l1 and not l2: return None l1_node, l2_node = l1, l2 l = dummy_new_head = ListNode(1) while l1_node and l2_node: if l1_node.val <= l2_node.val: l.next = ListNode(l1_node.val) l1_node = l1_node.next else: l.next = ListNode(l2_node.val) l2_node = l2_node.next l = l.next l.next = l1_node or l2_node return dummy_new_head.next if __name__ == "__main__": ListNode.print_linked_list(ListNode.build_linked_list([1,2,3])) #print_ListNode(Solution().mergeTwoLists(None, None)) #print_ListNode(Solution().mergeTwoLists(None, ListNode(1))) #print_ListNode(Solution().mergeTwoLists(build_ListNode([2]), build_ListNode([1]))) #print_ListNode(Solution().mergeTwoLists(build_ListNode([1]), build_ListNode([2]))) #print_ListNode(Solution().mergeTwoLists(build_ListNode([1,2,3]), build_ListNode([2, 5,7]))) ListNode.print_linked_list( Solution().mergeTwoLists( ListNode.build_linked_list([1, 2, 3]), ListNode.build_linked_list([2, 5, 7]) ) )
class Solution(object): def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ while head is not None and head.val == val: head = head.next if not head: return None current_node = head while current_node.next: if current_node.next.val == val: current_node.next = current_node.next.next else: current_node = current_node.next return head if __name__ == '__main__': ListNode.print_linked_list(Solution().removeElements( ListNode.build_linked_list([1, 2, 3, 4, 5, 6]), 6)) ListNode.print_linked_list(Solution().removeElements( ListNode.build_linked_list([1, 1, 3, 4, 56]), 1)) ListNode.print_linked_list(Solution().removeElements( ListNode.build_linked_list([2, 1, 3, 4, 4, 4, 4]), 4)) ListNode.print_linked_list(Solution().removeElements( ListNode.build_linked_list([1]), 1))
sorted_prev, sorted_current = sorted_head, sorted_head.next while sorted_current: if sorted_current.val > current.val: sorted_prev.next = current current.next = sorted_current break sorted_prev, sorted_current = sorted_current, sorted_current.next if current.val >= sorted_end.val: current.next = None sorted_end.next = current sorted_end = current current = current_next return sorted_head if __name__ == '__main__': ListNode.print_linked_list(Solution().insertionSortList( ListNode.build_linked_list([5, 1, 4, 3, 2]))) ListNode.print_linked_list(Solution().insertionSortList( ListNode.build_linked_list([1]))) ListNode.print_linked_list(Solution().insertionSortList( ListNode.build_linked_list([]))) ListNode.print_linked_list(Solution().insertionSortList( ListNode.build_linked_list([8, 7]))) ListNode.print_linked_list(Solution().insertionSortList( ListNode.build_linked_list([8, 7, 1]))) ListNode.print_linked_list(Solution().insertionSortList( ListNode.build_linked_list([1])))
:type l1: ListNode :type l2: ListNode :rtype: ListNode """ if not l1 and not l2: return None l1_node, l2_node = l1, l2 l = dummy_new_head = ListNode(1) while l1_node and l2_node: if l1_node.val <= l2_node.val: l.next = ListNode(l1_node.val) l1_node = l1_node.next else: l.next = ListNode(l2_node.val) l2_node = l2_node.next l = l.next l.next = l1_node or l2_node return dummy_new_head.next if __name__ == "__main__": ListNode.print_linked_list(ListNode.build_linked_list([1, 2, 3])) #print_ListNode(Solution().mergeTwoLists(None, None)) #print_ListNode(Solution().mergeTwoLists(None, ListNode(1))) #print_ListNode(Solution().mergeTwoLists(build_ListNode([2]), build_ListNode([1]))) #print_ListNode(Solution().mergeTwoLists(build_ListNode([1]), build_ListNode([2]))) #print_ListNode(Solution().mergeTwoLists(build_ListNode([1,2,3]), build_ListNode([2, 5,7]))) ListNode.print_linked_list(Solution().mergeTwoLists( ListNode.build_linked_list([1, 2, 3]), ListNode.build_linked_list([2, 5, 7])))
# reverse the list from the mid while current: next = current.next current.next = prev prev, current = current, next # interleave the two list current1, current2 = head, prev while current1 and current2: current1_next, current2_next = current1.next, current2.next current1.next, current2.next = current2, current1_next current1, current2 = current1_next, current2_next if __name__ == '__main__': l1 = ListNode.build_linked_list([1,2,3]) Solution().reorderList(l1) ListNode.print_linked_list(l1) l1 = ListNode.build_linked_list([1,2]) Solution().reorderList(l1) ListNode.print_linked_list(l1) l1 = ListNode.build_linked_list([]) Solution().reorderList(l1) ListNode.print_linked_list(l1) l1 = ListNode.build_linked_list([1]) Solution().reorderList(l1) ListNode.print_linked_list(l1) l1 = ListNode.build_linked_list([1,2,3,4,5,6]) Solution().reorderList(l1) ListNode.print_linked_list(l1) l1 = ListNode.build_linked_list([1,2,3,4,5,6,7]) Solution().reorderList(l1) ListNode.print_linked_list(l1)
return head elif m == n: return head c = 1 prev, current = None, head while current: if c == m: r_head_prev, r_head = prev, current prev, current = current, current.next elif m < c < n: next = current.next current.next = prev prev, current = current, next elif c == n: r_tail, r_tail_next = current, current.next current.next = prev prev, current = current, r_tail_next else: prev, current = current, current.next c += 1 if r_head_prev: r_head_prev.next = r_tail r_head.next = r_tail_next return head if m > 1 else r_tail if __name__ == '__main__': ListNode.print_linked_list(Solution().reverseBetween(ListNode.build_linked_list([1,2,3,4,5]), 2, 4)) ListNode.print_linked_list(Solution().reverseBetween(ListNode.build_linked_list([1,2,3,4,5]), 1, 4)) ListNode.print_linked_list(Solution().reverseBetween(ListNode.build_linked_list([1,2,3,4,5]), 1, 5)) ListNode.print_linked_list(Solution().reverseBetween(ListNode.build_linked_list([1,2,3,4,5]), 2, 2))
current = head while current: if current.val < x: if not f_head: f_head = current f_end = f_head else: f_end.next = current f_end = f_end.next else: if not b_head: b_head = current b_end = b_head else: b_end.next = current b_end = b_end.next current = current.next if f_head: f_end.next = b_head if b_end: b_end.next = None return f_head if f_head else b_head if __name__ == '__main__': ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1, 4, 3, 2, 5, 2]), 3)) ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1, 4, 3, 2, 5, 2]), 6)) ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1, 4, 3, 2, 5, 2]), 1)) ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1, 4, 3, 2, 5, 2]), 2)) ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1]), 2)) ListNode.print_linked_list(Solution().partition(None, 2))
while current: if c == m: r_head_prev, r_head = prev, current prev, current = current, current.next elif m < c < n: next = current.next current.next = prev prev, current = current, next elif c == n: r_tail, r_tail_next = current, current.next current.next = prev prev, current = current, r_tail_next else: prev, current = current, current.next c += 1 if r_head_prev: r_head_prev.next = r_tail r_head.next = r_tail_next return head if m > 1 else r_tail if __name__ == '__main__': ListNode.print_linked_list(Solution().reverseBetween( ListNode.build_linked_list([1, 2, 3, 4, 5]), 2, 4)) ListNode.print_linked_list(Solution().reverseBetween( ListNode.build_linked_list([1, 2, 3, 4, 5]), 1, 4)) ListNode.print_linked_list(Solution().reverseBetween( ListNode.build_linked_list([1, 2, 3, 4, 5]), 1, 5)) ListNode.print_linked_list(Solution().reverseBetween( ListNode.build_linked_list([1, 2, 3, 4, 5]), 2, 2))
:rtype: ListNode """ if not head: return None hash_counter = {} current = head while current: k = current.val hash_counter[k] = hash_counter.get(k, 0) + 1 current = current.next new_head = None prev, current = None, head while current: if hash_counter[current.val] >= 2: if prev: prev.next = current.next prev, current = prev, current.next else: if not new_head: new_head = current prev, current = current, current.next return new_head if __name__ == '__main__': ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1,1,2,3,1,1,2]))) ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1,1,2,3,1,1]))) ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1,2,3,3,4,4,5]))) ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1, 5]))) ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1]))) ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([])))
:type lists: List[ListNode] :rtype: ListNode """ from heapq import heapify, heappop, heappush h = [(l.val, l) for l in lists if l] heapify(h) new_head = current = None while len(h): val, node = heappop(h) if node.next: heappush(h, (node.next.val, node.next)) if not new_head: current = new_head = node else: current.next = node current = node node.next = None return new_head if __name__ == '__main__': ListNode.print_linked_list(Solution().mergeKLists( [b([1, 3, 5, 7, 9]), b([2, 4, 6, 8, 10]), b([12, 14, 15, 17, 19])])) ListNode.print_linked_list(Solution().mergeKLists( [b([1, 3, 5, 7, 9]), b([2, 4, 6, 8, 10]), b([])])) ListNode.print_linked_list(Solution().mergeKLists([b([]), b([])])) ListNode.print_linked_list(Solution().mergeKLists([]))
def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return None global new_head def reverse_list(node): global new_head if node.next is None: new_head = node return node next_node = reverse_list(node.next) next_node.next = node return node reverse_list(head) head.next = None return new_head if __name__ == '__main__': l = ListNode.build_linked_list([]) l = ListNode.build_linked_list([1]) # l = ListNode.build_linked_list([1, 2, 3, 4, 5]) ListNode.print_linked_list(Solution().reverseList(l))
class Solution(object): def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ from heapq import heapify, heappop, heappush h = [(l.val, l) for l in lists if l] heapify(h) new_head = current = None while len(h): val, node = heappop(h) if node.next: heappush(h, (node.next.val, node.next)) if not new_head: current = new_head = node else: current.next = node current = node node.next = None return new_head if __name__ == '__main__': ListNode.print_linked_list(Solution().mergeKLists([b([1,3,5,7,9]), b([2,4,6,8,10]), b([12,14,15,17,19])])) ListNode.print_linked_list(Solution().mergeKLists([b([1,3,5,7,9]), b([2,4,6,8,10]), b([])])) ListNode.print_linked_list(Solution().mergeKLists([b([]), b([])])) ListNode.print_linked_list(Solution().mergeKLists([]))
__author__ = 'clp' from utils import ListNode class Solution(object): def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ while head is not None and head.val == val: head = head.next if not head: return None current_node = head while current_node.next: if current_node.next.val == val: current_node.next = current_node.next.next else: current_node = current_node.next return head if __name__ == '__main__': ListNode.print_linked_list(Solution().removeElements(ListNode.build_linked_list([1,2,3,4,5,6]), 6)) ListNode.print_linked_list(Solution().removeElements(ListNode.build_linked_list([1,1,3,4,56]), 1)) ListNode.print_linked_list(Solution().removeElements(ListNode.build_linked_list([2,1,3,4,4,4,4]), 4)) ListNode.print_linked_list(Solution().removeElements(ListNode.build_linked_list([1]), 1))
# count the length and get last_node length = 0 current = head while current: length += 1 if current.next is None: last_node = current current = current.next k = k % length if k == 0: return head idx = 0 current = head while current: if idx == length - k - 1: target_prev, target_current = current, current.next current = current.next idx += 1 new_head = target_current last_node.next = head target_prev.next = None return new_head if __name__ == '__main__': ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 2)) ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 3)) ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 4)) ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 5)) ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 5)) ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2]), 2))
__author__ = 'clp' from utils import ListNode class Solution(object): def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ if node is None: return current_node = node while current_node.next: current_node.val = current_node.next.val if current_node.next.next: current_node = current_node.next else: current_node.next = None if __name__ == "__main__": l1 = ListNode.build_linked_list([1, 2, 3, 4]) l1_node_3 = l1.next.next Solution().deleteNode(l1_node_3) ListNode.print_linked_list(l1)
head.next = None while current: current_next = current.next if current.val < sorted_head.val: current.next = sorted_head sorted_head = current else: sorted_prev, sorted_current = sorted_head, sorted_head.next while sorted_current: if sorted_current.val > current.val: sorted_prev.next = current current.next = sorted_current break sorted_prev, sorted_current = sorted_current, sorted_current.next if current.val >= sorted_end.val: current.next = None sorted_end.next = current sorted_end = current current = current_next return sorted_head if __name__ == '__main__': ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([5,1,4,3,2]))) ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([1]))) ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([]))) ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([8,7]))) ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([8,7,1]))) ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([1])))