classSolution: defmergeKLists(self, lists: List[ListNode]) -> ListNode: stack = [] for singleNode in lists: while singleNode: stack.append(singleNode.val) singleNode = singleNode.next resNode = tem = ListNode(0) stack.sort() for a in stack: tem.next = ListNode(a) tem = tem.next return resNode.next
两两交换链表中的节点(24)
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
例:输入:head=[1,2,3,4]
输出:[2,1,4,3]
1 2 3 4 5 6 7 8 9
classSolution: defswapPairs(self, head: ListNode) -> ListNode: ifnot head ornot head.next: return head tail = head.next head.next = self.swapPairs(tail.next) tail.next = head return tail
classSolution: defsortList(self, head: ListNode) -> ListNode: ifnot head ornot head.next:return head slow, fast = head, head.next while fast and fast.next: slow, fast = slow.next, fast.next.next head1 = slow.next slow.next = None left, right = self.sortList(head), self.sortList(head1) dummy = cur = ListNode(0) while left and right: if left.val < right.val: cur.next = left left = left.next else: cur.next = right right = right.next cur = cur.next if left:cur.next = left if right:cur.next = right return dummy.next
classSolution: defhasCycle(self, head: ListNode) -> bool: if head == Noneor head.next == None: returnFalse slow = head fast = head.next while slow != fast: if fast == Noneor fast.next == None: returnFalse fast = fast.next.next slow = slow.next returnTrue
classSolution: defdetectCycle(self, head: ListNode) -> ListNode: if head isNone: returnNone if head.next isNone: returnNone first = second = head while second.next and second.next.next: first = first.next second = second.next.next if first == second: p = head while first != p: p = p.next first = first.next return p returnNone
classSolution: defreverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: if m == n: return head //留下头节点,方便操作 dummy = ListNode(0) dummy.next = head p = dummy //留下指针 for i in range(m - 1): p = p.next prev = p curr = p.next tail = curr //翻转要操作的部分 for i in range(n - m + 1): next = curr.next curr.next = prev prev = curr curr = next //连接节点 p.next = prev tail.next = curr return dummy.next
classSolution(object): //修改值 defswapPairs(self, head): p = head while p isnotNoneand p.next isnotNone: tmp = p.val p.val = p.next.val p.next.val = tmp p = p.next.next return head //修改指向 defswapPairs(self, head): if head == None: return head cur = ListNode(0) cur.next = head first =cur while cur.next and cur.next.next: n1 = cur.next n2 = n1.next nxt = n2.next n1.next = nxt n2.next = n1 cur.next = n2
cur = n1 return first.next
K个一组列表翻转(25)
给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
k 是一个正整数,它的值小于或等于链表的长度。
如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
例:输入:head=[1,2,3,4,5],k=3
输出:[3,2,1,4,5]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution: defreverseKGroup(self, head: ListNode, k: int) -> ListNode: cur = head count = 0 while cur and count!= k: cur = cur.next count += 1 if count == k: cur = self.reverseKGroup(cur, k) while count: tmp = head.next head.next = cur cur = head head = tmp count -= 1 head = cur return head
classSolution: defdeleteDuplicates(self, head: ListNode) -> ListNode: # 判断链表是否为空 ifnot head: return head # 当前节点初始化为表头 cur = head while cur and cur.next:# 遍历,循环条件为当前节点和当前节点的后继不为空 if cur.val == cur.next.val:# 如果当前节点值和其后继值相等,则将其后继改为后继的后继 cur.next = cur.next.next# 如果不相等,则将当前节点更新 else: cur = cur.next return head
classSolution: defdeleteDuplicates(self, head: ListNode) -> ListNode: #判断链表是否为空 ifnot head: return head pre = None# 设置两个指针:pre和cur cur = head # 遍历所有节点 while cur: # 一个临时指针指向cur的后继 _next = cur.next ifnot _next: return head # 如果两个值不相等,则将pre和cur都往后移 elif cur.val != _next.val: pre = cur cur = _next continue # 如果相等,则将_next一直捋到不重复为止 else: while _next and _next.val == cur.val: _next = _next.next if cur == head:# 如果重复段在表头,则需要更新表头信息 head = _next else:# 如果重复段在中间,则需要将pre的后继更新为_next pre.next = _next # 继续遍历 cur = _next return head
分隔链表(86)
给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。
你应当保留两个分区中每个节点的初始相对位置。
例:输入:head = [1,4,3,2,5,2],x = 3
输出:[1,2,2,4,3,5]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: defpartition(self, head: ListNode, x: int) -> ListNode: dummy1 = ListNode(0) dummy2 = ListNode(0) p1, p2 = dummy1, dummy2 cur = head while cur: if cur.val < x: p1.next = cur p1 = p1.next else: p2.next = cur p2 = p2.next #print(cur.val) cur = cur.next p2.next = None p1.next = dummy2.next return dummy1.next
classSolution: defreorderList(self, head: ListNode) -> None: ifnot head ornot head.next: return head fast, slow = head, head #找到中点 while fast.next and fast.next.next: fast = fast.next.next slow = slow.next #反转后半链表 p, right = slow.next, None slow.next = None while p: right, right.next, p = p, right, p.next #重排练表 left = head while left and right: left.next,right.next,left,right = right,left.next,left.next,right.next
两个链表的第一个公共节点(剑指offer52)
输入两个链表,找出它们的第一个公共节点。
1 2 3 4 5 6 7
classSolution: defgetIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: A, B = headA, headB while A != B: A = A.next if A else headB B = B.next if B else headA return A
回文链表(234)
请判断一个链表是否为回文链表。
例:输入:1->2->2->1
输出:true
转数组,时间复杂度和空间复杂度都是O(n)
1 2 3 4 5 6 7 8 9 10
classSolution: defisPalindrome(self, head: ListNode) -> bool: a = [] while head isnotNone: a.append(head.val) head = head.next for i in range(len(a)): if a[i] != a[-1-i]: returnFalse returnTrue
利用快慢指针将后半部分翻转,然后进行比较,O(n) 时间复杂度和 O(1) 空间复杂度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution: defisPalindrome(self, head: ListNode) -> bool: ifnot head: returnTrue slow = fast = head pre = None # 快指针指向下两个,这样遍历之后,slow只走到中间节点,同时翻转后半部分链表 while fast and fast.next: fast = fast.next.next slow.next, pre, slow = pre, slow , slow.next if fast: slow = slow.next #进行比较 while pre: if pre.val != slow.val: returnFalse pre = pre.next slow = slow.next returnTrue
classSolution: defsplitListToParts(self, root: ListNode, k: int) -> List[ListNode]: total_len = 0 head = root while head: total_len += 1 head = head.next ans = [Nonefor _ in range(k)] l, r = total_len // k, total_len % k prev, head = None, root for i in range(k): ans[i] = head for j in range(l + (1if r > 0else0)): prev, head = head, head.next if prev: prev.next = None r -= 1 return ans
classSolution: defnextLargerNodes(self, head: ListNode) -> List[int]: ifnot head: return [] nums = [] cur = head while cur: nums.append(cur.val) cur=cur.next stack = [0] res = [0]*len(nums) for i in range(1,len(nums)): while stack and nums[i]>nums[stack[-1]]: res[stack[-1]]=nums[i] stack.pop() stack.append(i) return res