tails[0].nxt = odd_head return even_head def get_input(case=0): def gen_even_odd_array(x): res = [] for i in xrange(0, len(x), 2): res.append(x[i]) for i in xrange(1, len(x), 2): res.append(x[i]) return res src = range(case + 1) ans_src = gen_even_odd_array(src) head = gen_list(src) ans = gen_list(ans_src) return head, ans def main(): for case in xrange(7): print('--- case {} ---'.format(case)) head, ans = get_input(case) print('Input:') print('head =', get_list_str(head)) res = even_odd_merge(head) print('Output:') print('res =', get_list_str(res)) print('ans =', get_list_str(ans)) print('Test success' if is_equal(res, None, ans, None
data_1 = node_1.data if node_1 else 0 data_2 = node_2.data if node_2 else 0 tmp = data_1 + data_2 + ca res_node.nxt = Node(tmp % 10, None) ca = tmp / 10 if node_1: node_1 = node_1.nxt if node_2: node_2 = node_2.nxt res_node = res_node.nxt if ca > 0: res_node.nxt = Node(ca, None) return res_head.nxt def get_input(case=0): if case == 0: head_1 = gen_list([3, 1, 4]) head_2 = gen_list([7, 0, 9]) ans = gen_list([0, 2, 3, 1]) return head_1, head_2, ans elif case == 1: head_1 = gen_list([9, 4]) head_2 = gen_list([7, 0, 9]) ans = gen_list([6, 5, 9]) return head_1, head_2, ans elif case == 2: head_2 = gen_list([9, 4]) head_1 = gen_list([7, 0, 9]) ans = gen_list([6, 5, 9]) return head_1, head_2, ans elif case == 3: head_1 = gen_list([9, 9])
return False first_half_iter = first_half_iter.nxt second_half_iter = second_half_iter.nxt """ you can reverse 2nd half sublist back to original here, depends on the interviewr's opinion: second_half_head = reverse_list(second_half_head) """ return True def get_input(case=0): if case == 0: return None, True elif case == 1: return gen_list([1]), True elif case == 2: return gen_list([1, 1]), True elif case == 3: return gen_list([1, 2]), False elif case == 4: return gen_list([7, 3, 1, 5, 1, 3, 7]), True elif case == 5: return gen_list([7, 3, 1, 5, 2, 3, 7]), False def main(): print('Use is_palindromic =====') for case in xrange(6): print('--- case {} ---'.format(case)) head, ans = get_input(case)
dummy_head = Node(0, head) end = dummy_head.nxt while k > 0: end = end.nxt k -= 1 start = dummy_head while end: start = start.nxt end = end.nxt start.nxt = start.nxt.nxt return dummy_head.nxt def get_input(case=0): if case == 0: head = gen_list([1]) k = 1 ans = None len_ans = 0 return head, k, ans, len_ans elif case == 1: head = gen_list([1, 2]) k = 1 ans = gen_list([1]) len_ans = 1 return head, k, ans, len_ans elif case == 2: head = gen_list([1, 2, 3]) k = 1 ans = gen_list([1, 2]) len_ans = 2
equal_iter = equal_iter.nxt elif node.data > k: greater_iter.nxt = node greater_iter = greater_iter.nxt node = node.nxt if not equal_head.nxt: return head # the pivot does not exist, just return the original head less_iter.nxt = equal_head.nxt equal_iter.nxt = greater_head.nxt greater_iter.nxt = None return less_head.nxt def get_input(case=0): if case == 0: head = gen_list([3, 2, 2, 11, 7, 5, 11]) ans = gen_list([3, 2, 2, 5, 7, 11, 11]) k = 7 return head, k, ans elif case == 1: src = [3, 2, 2, 11, 7, 5, 11] head = gen_list(src) ans = gen_list(src) k = 100 return head, k, ans elif case == 2: head = gen_list([3, 2, 19, 2, 11, 7, 8, 5, 11, 4, 7]) ans = gen_list([3, 2, 2, 5, 4, 7, 7, 19, 11, 8, 11]) k = 7 return head, k, ans elif case == 3:
def delete_a_node_which_is_not_a_tail(node): """ This problem assumes the node to delete is not a tail. The time complexity is O(1). The additional complexity complexity is O(1). """ node.data = node.nxt.data node.nxt = node.nxt.nxt def get_input(case=0): src = [1, 2, 3] if case == 0: head = gen_list(src) node = head ans = gen_list([2, 3]) return head, node, ans elif case == 1: head = gen_list(src) node = head.nxt ans = gen_list([1, 3]) return head, node, ans def main(): for case in xrange(2): head, node, ans = get_input(case) print('--- case {} ---'.format(case)) print('Input:')