def reverseKGroup(self, head: ListNode, k: int) -> ListNode: def reverse(node): if not node.next: return node next = reverse(node.next) next.next = node return node if k == 1: return head virtual_head = ListNode() virtual_head.next = head pre_head = virtual_head head = virtual_head.next while True: # print(head.val) if not head: break temp = head temp_num = 1 while temp_num != k and temp.next: temp = temp.next temp_num += 1 if temp_num < k: break else: # 记录尾部节点和after尾部节点 tail = temp after_tail = temp.next tail.next = None reverse(head) # 将逆转之后的头节点跟前一个节点链接 尾节点跟后一个链接 pre_head.next = tail head.next = after_tail # 记录新的头 pre_head = head head = after_tail return virtual_head.next
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if not l1 and not l2: return None if not l2: l1.next = self.mergeTwoLists(l1.next, l2) return l1 if not l1: l2.next = self.mergeTwoLists(l1, l2.next) return l2 if l1.val < l2.val: l1.next = self.mergeTwoLists(l1.next, l2) return l1 else: l2.next = self.mergeTwoLists(l1, l2.next) return l2
def sortList(self, head: ListNode) -> ListNode: if not head: return None # print('head = %d' % head.val) if not head.next: return head # 获取长度 length = 0 index = head while index: index = index.next length = length + 1 mid = int(length / 2) # print('length = %d' % length) # 获取中间节点 index = head mid_index = 0 while index: mid_index = mid_index + 1 if mid_index == mid: break index = index.next # print('mid_index = %d' % index.val) # 截断变成两节 right = index.next index.next = None # 将左右分别排序 left = self.sortList(head) right = self.sortList(right) # print('left = %d, left.next is %s, right = %d, right.next is %s' % (left.val, left.next, right.val, right.next)) # 合并 temp = ListNode(0) current = temp while left or right: # print('current = %d, left is %s, right is %s' % (current.val, left, right)) if left and right: if left.val < right.val: current.next = left left = left.next else: current.next = right right = right.next else: if not left: current.next = right right = right.next else: current.next = left left = left.next current = current.next return temp.next
def partition(self, head: ListNode, x: int) -> ListNode: min_head = ListNode(0) max_head = ListNode(0) min_tail = min_head max_tail = max_head index = head while index: if index.val >= x: max_tail.next = ListNode(index.val) max_tail = max_tail.next else: min_tail.next = ListNode(index.val) min_tail = min_tail.next index = index.next min_tail.next = max_head.next return min_head.next
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: if not head.next: return None num = 0 vir_head = ListNode(0) vir_head.next = head temp = vir_head real_temp = vir_head while temp.next: num += 1 temp = temp.next if num > n: real_temp = real_temp.next if real_temp.next == head: return head.next else: real_temp.next = real_temp.next.next return head
def reorderList(self, head: ListNode) -> None: """ Do not return anything, modify head in-place instead. """ # 获取长度 length = 0 index = head while index: index = index.next length = length + 1 mid = int(length / 2) # print('length = %d' % length) # 获取中间节点 index = head mid_index = 0 while index: mid_index = mid_index + 1 if mid_index == mid: break index = index.next # print('mid_index = %d' % index.val) # 截断变成两节 right = index.next index.next = None # 反转后节 current = None next = right while next: next_next = next.next next.next = current current = next next = next_next # 合共两节 new_head = ListNode(0) index = new_head while current or head: if head: index.next = head index = index.next head = head.next if current: index.next = current index = index.next current = current.next print('ok')
if left and right: if left.val < right.val: current.next = left left = left.next else: current.next = right right = right.next else: if not left: current.next = right right = right.next else: current.next = left left = left.next current = current.next return temp.next if __name__ == '__main__': solution = Solution() one = ListNode(4) two = ListNode(2) thr = ListNode(1) fou = ListNode(3) one.next = two two.next = thr thr.next = fou print(solution.sortList(one))
def mergeKLists(self, lists: List[ListNode]) -> ListNode: if not lists: return None def push(val, node): heap.append((val, node)) pos = len(heap) - 1 while pos > 0: father = (pos - 1) >> 1 parent_val, parent_node = heap[father] if parent_val > val: heap[pos] = (parent_val, parent_node) pos = father continue break heap[pos] = (val, node) def pop(): last_val, last_node = heap.pop() if heap: return_val, return_node = heap[0] pos = 0 child = 2 * pos + 1 while child < len(heap): right_child = child + 1 if right_child < len( heap) and heap[right_child][0] < heap[child][0]: child = right_child if heap[child][0] < last_val: heap[pos] = heap[child] pos = child child = 2 * pos + 1 else: break heap[pos] = (last_val, last_node) return (return_val, return_node) return (last_val, last_node) heap = [] for node in lists: if node: push(node.val, node) head = ListNode() temp = head while heap: val, node = pop() if node.next: push(node.next.val, node.next) temp.next = node temp = temp.next return head.next
def detectCycle(self, head: ListNode) -> ListNode: if not head: return None found = set() while head: found.add(head) # print(head.val) # print(found) if head.next and head.next in found: return head.next head = head.next return None if __name__ == '__main__': solution = Solution() one = ListNode(1) two = ListNode(2) thr = ListNode(3) fou = ListNode(4) one.next = two two.next = thr thr.next = fou fou.next = two print(solution.detectCycle(one))
if not head: return tempHead = head li = [] while tempHead: li.append(tempHead) tempHead = tempHead.next x, y = 0, len(li) - 1 while x < y: li[x].next = li[y] x += 1 if x >= y: break li[y].next = li[x] y -= 1 if x >= y: li[x].next = None if __name__ == '__main__': # 测试用例 # 构建链表 newList = ListNode.create([1, 2, 3, 4, 5, 6, 7, 8, 9]) # 输出转换之前链表 print(ListNode.toList(newList)) # 进行转换 Solution().reorderList(newList) # 输出转换之后列表 print(ListNode.toList(newList))
""" 反转链表 :param head: 头节点 :return:新头节点 """ if head is None or head.next is None: return lastList = None midList = head nextList = midList.next while midList.next is not None: midList.next = lastList lastList = midList midList = nextList nextList = midList.next midList.next = lastList return midList if __name__ == "__main__": # 测试用例 # 新建一个链表 newList = ListNode.create([0, 1, 2, 3, 4]) # 输出原先的链表 print(ListNode.toList(newList)) # 反转链表 newList = Solution().reverseList(newList) # 输出新的链表 print(ListNode.toList(newList))
next.next = current current = next next = next_next # 合共两节 new_head = ListNode(0) index = new_head while current or head: if head: index.next = head index = index.next head = head.next if current: index.next = current index = index.next current = current.next print('ok') if __name__ == '__main__': solution = Solution() one = ListNode(1) # two = ListNode(2) # thr = ListNode(3) # fou = ListNode(4) # # one.next = two # two.next = thr # thr.next = fou print(solution.reorderList(one))