def remove_duplicates(data): if data is None: return None if len(data) == 0: return [] head = List.fromvalues(data).head return list(List.fromhead(_remove_duplicates(head)))
def delete(data, val): if data is None: return None if len(data) == 0: return [] head = List.fromvalues(data).head p = head.next while p and p.val != val: p = p.next return list(List.fromhead(_delete_node(head, p)))
def _(*args, **kwargs): head = func(*args, **kwargs) if head is not None: return list(List.fromhead(head)) return None
while q and steps > 0: q = q.next steps -= 1 # 二者一起往后走,直到遇到相同的节点 while p and q: if p is q: return p.val p = p.next q = q.next return None def _get_length(head): if head is None: return 0 return head.val if __name__ == '__main__': h1, h2 = build_two_list_with_common_nodes([1, 2], [4, 5, 6], common_nodes=[10, 11, 12]) print(List.fromhead(h1)) print(List.fromhead(h2)) print(find_first_common_node_2(h1, h2)) print(_get_length(h1))