class Solution(object): def partition(self, head, x): """ https://discuss.leetcode.com/topic/7005/very-concise-one-pass-solution/20 The idea is to just iterate over all the nodes and move nodes less than x to first_half list and others to second_half list. In the end join first_half end to second_half start and nullify second_half end """ first_half = start = ListNode('first_half') second_half = second_start = ListNode('second_half') while head: if head.val < x: first_half.next = head first_half = first_half.next else: second_half.next = head second_half = second_half.next head = head.next first_half.next = second_start.next # Note: Close the second half. This is imp. since existing links can cause infinite loops second_half.next = None return start.next if __name__ == '__main__': arr, x = [2, 1], 2 from linkedlistbase import construct_linked_list_from_array, print_linked_list head = construct_linked_list_from_array(arr) res = Solution().partition(head, x) print_linked_list(res)
def merge_k_lists_naive2(self, lists): # Time: O(nk*k) where k is the number of lists, with each list having n nodes # since we'll be iterating for a total of nk nodes over k lists orig_head = cur_head = ListNode('dummy') while True: cur_min = ListNode(float('inf')) for index, node in enumerate(lists): if node is None: continue if node.val < cur_min.val: cur_min = node min_index = index if cur_min.val == float('inf'): return orig_head.next else: cur_head.next = cur_min lists[min_index] = cur_min.next cur_head = cur_head.next return orig_head.next if __name__ == '__main__': arr_list = [[10, 15, 25], [5, 12, 20], [1, 4, 7, 11, 17, 22]] # arr_list = [] sorted_list = [] for arr in arr_list: sorted_list.append(construct_linked_list_from_array(arr)) # print_linked_list(merge_k_sorted_lists(sorted_list)) # print_linked_list(merge_k_sorted_lists_using_heap(sorted_list)) print_linked_list(merge_k_sorted_lists_optimized(sorted_list))
rotate 4 steps to the right: 2->0->1->NULL Idea:https://discuss.leetcode.com/topic/14470/my-clean-c-code-quite-standard-find-tail-and-reconnect-the-list/22 """ if not head or not k or not head.next: return head # get LL length len = 1 orig_head = head while head.next is not None: head = head.next len += 1 head.next = orig_head # circle the LL k = k % len head = orig_head for _ in xrange( len - k - 1 ): # when the head is at len-k-1 node(i.e just before our end node) head = head.next orig_head = head.next head.next = None return orig_head if __name__ == '__main__': arr, r = [1, 2], 1 head = construct_linked_list_from_array(arr) print_linked_list(head) print new_head = Solution().rotateRight(head, r) print_linked_list(new_head)
if current_node.next is None: current_node.next = next_node new_head = current_node elif current_node.next.next is None: next_node_backup = next_node next_node = current_node.next current_node.next = next_node_backup next_node.next = current_node new_head = next_node return new_head if __name__ == '__main__': inp_arr = [1, 2, 3, 4] # inp_arr = [1, 2, 3, 4, 5, 6] # inp_arr = [1] # inp_arr = [1, 2] # inp_arr = [1, 2, 3] # m = 4 # n = 4 # head = construct_linked_list_from_array(inp_arr) # print_linked_list(head) # print # print_linked_list(reverse_linked_list_with_params(head, m, n)) # print_linked_list(reverse_linked_list(head)) head = construct_linked_list_from_array(inp_arr) print_linked_list(head) print sol = Solution() print_linked_list(sol.reverse_linked_list_recursion(head))