classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defprint_list_from_tail_to_head(self, head): ifnot head: return [] res = [] while head: res.append(head.val) head = head.next return res[::-1]
if __name__ == "__main__": head = ListNode(1) a = ListNode(2) b = ListNode(3) head.next = a a.next = b print(Solution().print_list_from_tail_to_head(head))
[3, 2, 1]
思路2(反转链表): 先将链表反转,然后遍历链表,将节点的值加入数组。
时间复杂度O(n),空间复杂度O(n)
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defprint_list_from_tail_to_head(self, head): ifnot head: return [] cur = head pre = None while cur: temp = cur.next cur.next = pre pre = cur cur = temp res = [] point = pre while point: res.append(point.val) point = point.next return res
if __name__ == "__main__": head = ListNode(1) a = ListNode(2) b = ListNode(3) head.next = a a.next = b print(Solution().print_list_from_tail_to_head(head))
[3, 2, 1]
18.1 删除链表节点
题目描述
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点,返回删除后的链表的头节点。
Input: {2, 5, 1, 9}, 5
Output: {2, 1, 9}
题解
思路(穿针引线): 先找到要删除的元素,然后删除点,需要考虑要删除的节点为头节点的特殊情况。
时间复杂度O(n),空间复杂度O(1)
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defdelete_node(self, head, val): cur = head if cur.val == val: return head.next while cur and cur.next: if cur.next.val == val: cur.next = cur.next.next cur = cur.next return head
if __name__ == "__main__": head = ListNode(2) a = ListNode(5) b = ListNode(1) c = ListNode(9) head.next = a a.next = b b.next = c val = 5 cur = Solution().delete_node(head, val) while cur: print(cur.val) cur = cur.next
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defdelete_duplication(self, head): ifnot head: returnNone res = ListNode(0) res.next = head cur = res while cur.nextand cur.next.next: if cur.next.val == cur.next.next.val: temp = cur.next.val while cur.nextand cur.next.val == temp: cur.next = cur.next.next else: cur = cur.next return res.next
if __name__ == "__main__": head = ListNode(1) a = ListNode(2) b = ListNode(3) c = ListNode(3) d = ListNode(4) e = ListNode(4) f = ListNode(5) head.next = a a.next = b b.next = c c.next = d d.next = e e.next = f cur = Solution().delete_duplication(head) while cur: print(cur.val) cur = cur.next
1
2
5
思路2(哈希表): 使用哈希表辅助统计是否重复。
时间复杂度O(n),空间复杂度O(n)
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defdelete_duplication(self, head): ifnot head: returnNone hash_map = dict() cur = head while cur: if cur.val notin hash_map: hash_map[cur.val] = 1 else: hash_map[cur.val] += 1 cur = cur.next res = ListNode(0) res.next = head cur = res while cur and cur.next: if hash_map[cur.next.val] != 1: cur.next = cur.next.next else: cur = cur.next return res.next
if __name__ == "__main__": head = ListNode(1) a = ListNode(2) b = ListNode(3) c = ListNode(3) d = ListNode(4) e = ListNode(4) f = ListNode(5) head.next = a a.next = b b.next = c c.next = d d.next = e e.next = f cur = Solution().delete_duplication(head) while cur: print(cur.val) cur = cur.next
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: deffind_kth_to_tail(self, head, k): fast = head slow = head for i inrange(k): if fast: fast = fast.next else: returnNone while fast: fast = fast.next slow = slow.next return slow
if __name__ == "__main__": head = ListNode(1) a = ListNode(2) b = ListNode(3) c = ListNode(4) d = ListNode(5) head.next = a a.next = b b.next = c c.next = d k = 2 cur = Solution().find_kth_to_tail(head, k) while cur: print(cur.val) cur = cur.next
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defentry_node_of_loop(self, head): fast = head slow = head whileTrue: ifnot fast ornot fast.next: returnNone fast = fast.next.next slow = slow.next if fast == slow: break fast = head while fast != slow: fast = fast.next slow = slow.next return slow
if __name__ == "__main__": head = ListNode(1) a = ListNode(6) b = ListNode(3) c = ListNode(5) d = ListNode(2) e = ListNode(4) head.next = a a.next = b b.next = c c.next = d d.next = e e.next = b cur = Solution().entry_node_of_loop(head) print(cur.val)
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defreverse_list(self, head): ifnot head ornot head.next: return head pre = None cur = head while cur: temp = cur.next cur.next = pre pre = cur cur = temp return pre
if __name__ == "__main__": head = ListNode(2) a = ListNode(1) b = ListNode(5) c = ListNode(3) d = ListNode(4) head.next = a a.next = b b.next = c c.next = d cur = Solution().reverse_list(head) while cur: print(cur.val) cur = cur.next
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defreverse_list(self, head): ifnot head ornot head.next: return head temp = head.next head.next = None new_head = self.reverse_list(temp) temp.next = head return new_head
if __name__ == "__main__": head = ListNode(2) a = ListNode(1) b = ListNode(5) c = ListNode(3) d = ListNode(4) head.next = a a.next = b b.next = c c.next = d cur = Solution().reverse_list(head) while cur: print(cur.val) cur = cur.next
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defmerge(self, head1, head2): ifnot head1: return head2 ifnot head2: return head1 p = ListNode(-1) q = p while head1 and head2: if head1.val < head2.val: p.next = head1 head1 = head1.next else: p.next = head2 head2 = head2.next p = p.next if head1: p.next = head1 if head2: p.next = head2 return q.next
if __name__ == "__main__": head1 = ListNode(1) a = ListNode(3) b = ListNode(4) c = ListNode(10) d = ListNode(18) head1.next = a a.next = b b.next = c c.next = d head2 = ListNode(2) e = ListNode(3) f = ListNode(7) g = ListNode(8) h = ListNode(9) head2.next = e e.next = f f.next = g g.next = h cur = Solution().merge(head1, head2) while cur: print(cur.val) cur = cur.next
if __name__ == "__main__": head1 = ListNode(1) a = ListNode(3) b = ListNode(4) c = ListNode(10) d = ListNode(18) head1.next = a a.next = b b.next = c c.next = d head2 = ListNode(2) e = ListNode(3) f = ListNode(7) g = ListNode(8) h = ListNode(9) head2.next = e e.next = f f.next = g g.next = h cur = Solution().merge(head1, head2) while cur: print(cur.val) cur = cur.next
classSolution: defclone(self, head): ifnot head: return head cur = head while cur: clone = RandomListNode(cur.label) clone.next = cur.next cur.next = clone cur = clone.next cur = head clone = head.next res = head.next while cur: ifnot cur.random: clone.random = None else: clone.random = cur.random.next cur = cur.next.next if clone.next: clone = clone.next.next cur = head clone = head.next while cur: cur.next = cur.next.next cur = cur.next if clone.next: clone.next = clone.next.next clone = clone.next return res
if __name__ == "__main__": head = RandomListNode(1) a = RandomListNode(2) b = RandomListNode(3) head.next = a a.next = b head.random = b a.random = head b.ramdom = head p = Solution().clone(head) cur = p while cur: print(cur.label) cur = cur.next while p: if p.random: print(p.random.label) p = p.next
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: deffind_first_common_node(self, head1, head2): ifnot head1 ornot head2: returnNone p, q = head1, head2 while p != q: p = p.nextif p else head2 q = q.nextif q else head1 return p
if __name__ == "__main__": head1 = ListNode(1) a = ListNode(2) b = ListNode(3) head1.next = a a.next = b head2 = ListNode(4) c = ListNode(5) head2.next = c e = ListNode(6) f = ListNode(7) b.next = e e.next = f c.next = e cur = Solution().find_first_common_node(head1, head2) while cur: print(cur.val) cur = cur.next