classSolution: defreverseList(self, head: ListNode) -> ListNode: # 迭代 cur, pre = head, None while cur: tmp = cur.next# 暂存后继节点 cur.next cur.next = pre # 修改 next 引用指向 pre = cur # pre 暂存 cur cur = tmp # cur 访问下一节点 return pre defreverseList(self, head: ListNode) -> ListNode: # 递归 defrecur(cur, pre): ifnot cur: return pre # 终止条件 res = recur(cur.next, cur) # 递归后继节点 cur.next = pre # 修改节点引用指向 return res # 返回反转链表的头节点 return recur(head, None) # 调用递归并返回
请实现 copyRandomList
函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next
指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者
null。
defaultdict(),空为0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defcopyRandomList(self, head: 'Node') -> 'Node': # 拼接+拆分 【哈希表】 ifnot head: return head dic = collections.defaultdict() cur = head while cur: dic[cur] = Node(cur.val) cur = cur.next cur = head while cur: dic[cur].next = dic[cur.next] if cur.nextelseNone dic[cur].random = dic[cur.random] if cur.random elseNone# keyerror cur = cur.next return dic[head]
给定一个链表,删除链表的倒数第 n
个结点,并且返回链表的头结点。
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defremoveNthFromEnd(self, head: ListNode, n: int) -> ListNode: dummy = ListNode(0, head) first = head second = dummy for i inrange(n): first = first.next while first: first = first.next second = second.next second.next = second.next.next return dummy.next
给定一个链表,返回链表开始入环的第一个节点。 从链表的头节点开始沿着
next 指针进入环的第一个节点为环的入口节点。如果链表无环,则返回
null。
classSolution: defdetectCycle(self, head: ListNode) -> ListNode: fast = slow = head while fast and fast.next: fast = fast.next.next slow = slow.next if fast == slow: fast = head while fast != slow: fast = fast.next slow = slow.next return slow returnNone
classSolution: defaddTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: defreverseList(head: ListNode) -> ListNode: ifnot head: return head pre, cur = None, head while cur: tmp = cur.next cur.next = pre pre = cur cur = tmp return pre
ifnot l1 andnot l2: return l1 = reverseList(l1) l2 = reverseList(l2) more = 0 p = None while l1 or l2: # 倒序添加 val1 = l1.val if l1 else0 val2 = l2.val if l2 else0 total = val1 + val2 + more more, cur = divmod(total, 10) p = ListNode(cur, p) l1 = l1.nextif l1 else l1 l2 = l2.nextif l2 else l2 if more != 0: p = ListNode(more, p) return p
classSolution: defreorderList(self, head: ListNode) -> None: """ Do not return anything, modify head in-place instead. """ defreverseList(head: ListNode) -> ListNode: pre, cur = None, head while cur: tmp = cur.next cur.next = pre pre = cur cur = tmp return pre fast, slow = head.next, head while fast and fast.next: slow, fast = slow.next, fast.next.next mid, slow.next = slow.next, None left, cur = head, reverseList(mid) slow.next = None while cur: tmp = cur.next cur.next = left.next left.next = cur left = cur.next cur = tmp return head
classSolution: defisPalindrome(self, head: ListNode) -> bool: defreverseList(head: ListNode) -> ListNode: pre, cur = None, head while cur: tmp = cur.next cur.next = pre pre = cur cur = tmp return pre slow = fast = head while fast.nextand fast.next.next: slow, fast = slow.next, fast.next.next left, right = head, slow.next slow.next = None right = reverseList(right) while left and right: if left.val != right.val: returnFalse left, right = left.next, right.next returnTrue
classSolution: defflatten(self, head: 'Node') -> 'Node': # 栈迭代 if head == None: return head dummy = Node(-1, None, None, None) pre = dummy stk = [head] while stk: x = stk.pop() pre.next = x x.prev = pre if x.next: stk.append(x.next) if x.child: stk.append(x.child) x.child = None pre = x dummy.next.prev = None return dummy.next defflatten(self, head: 'Node') -> 'Node': if head == None: return head dummy = Node(-1, None, None, None) defdfs(pre: 'Node', cur: 'Node') -> 'Node': if cur == None: return pre pre.next = cur cur.prev = pre