def hasCycle2(self, head: ListNode) -> bool: """ 不需要额外空间 """ while head: if head.val is None: return True head.val = None # listNode 的值不会为 None,所以这里赋值以后如果下一个循环 # head.val 为 None,则认为进入了循环 head = head.next return False
def add_two_numbers(l1, l2): s1, s2 = [], [] tmp1 = l1 while tmp1: s1.append(tmp1.val) tmp1 = tmp1.next tmp1 = l2 while tmp1: s2.append(tmp1.val) tmp1 = tmp1.next head = ListNode(0) sum = 0 while s1 or s2: if s1: sum += s1.pop() if s2: sum += s2.pop() head.val = sum % 10 # assign the current value to the exist node and create a new node for the carry value cur = ListNode(sum // 10) cur.next = head head = cur sum //= 10 return head if head.val != 0 else head.next
def deleteNode(self, node: ListNode): node.val = node.next.val node.next = node.next.next
def delete_node(node: ListNode): node.val, node.next = node.next.val, node.next.next