dummy = ListNode(-1) dummy.next = head prev_node = dummy while head and head.next: # Nodes to be swapped first_node = head second_node = head.next # Swapping prev_node.next = second_node first_node.next = second_node.next second_node.next = first_node # Reinitializing the head and prev_node for next swap prev_node = first_node head = first_node.next # Return the new head node. return dummy.next if __name__ == '__main__': head = generateList([1, 2, 3, 4, 5]) solution = Solution() printList(solution.swapPairs_1(head)) head = generateList([1, 2, 3, 4, 5]) printList(solution.swapPairs_2(head))
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def removeElements(self, head: ListNode, val: int) -> ListNode: sentinel = ListNode(0) sentinel.next = head prev, curr = sentinel, head while curr: if curr.val == val: prev.next = curr.next else: prev = curr curr = curr.next return sentinel.next if __name__ == '__main__': head = generateList([1, 2, 6, 3, 4, 5, 6]) val = 6 solution = Solution() printList(solution.removeElements(head, val))
# Restore the list and return the result. first_half_end.next = self.reverse_list(second_half_start) return result def end_of_first_half(self, head): fast = head slow = head while fast.next is not None and fast.next.next is not None: fast = fast.next.next slow = slow.next return slow def reverse_list(self, head): previous = None current = head while current is not None: next_node = current.next current.next = previous previous = current current = next_node return previous if __name__ == '__main__': head = generateList([1, 2, 3, 3, 2, 1]) solution = Solution() print(solution.isPalindrome_1(head)) print(solution.isPalindrome_2(head)) print(solution.isPalindrome_3(head))
def __init__(self, x): self.val = x self.next = None class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: pre = ListNode(0) head = pre while l1 and l2: if l1.val <= l2.val: pre.next = l1 l1 = l1.next else: pre.next = l2 l2 = l2.next pre = pre.next pre.next = l1 if l1 else l2 return head.next if __name__ == '__main__': l1 = generateList([1, 2, 4]) l2 = generateList([1, 3, 4]) solution = Solution() printList(l1) printList(l2) mergeList = solution.mergeTwoLists(l1, l2) printList(mergeList)
""" 单指针遍历 间复杂度 O(N) : N 为链表长度,删除操作平均需循环 N/2 次,最差 N 次。 空间复杂度 O(1) : pre 占用常数大小额外空间。 :param head: :param val: :return: """ if not head: return if head.val == val: return head.next pre = ListNode(0) pre.next = head while pre.next: if pre.next.val == val: pre.next = pre.next.next break pre = pre.next return head if __name__ == '__main__': head = generateList([-3, 5, -99]) val = -3 solution = Solution() printList(solution.deleteNode(head, val))
def home(): dir = generateList(basedir) return render_template('index.html', dir=dir)
def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def oddEvenList(self, head: ListNode) -> ListNode: if not head: return None odd = head even = head.next evenHead = even while even and even.next: odd.next = even.next odd = odd.next even.next = odd.next even = even.next odd.next = evenHead return head if __name__ == '__main__': head = generateList([1, 2, 3, 4, 5, 6, 7, 8, 9]) solution = Solution() printList(solution.oddEvenList(head))
:param head: :return: """ if not head or not head.next: return head # termination. # cut the LinkedList at the mid index. slow, fast = head, head.next while fast and fast.next: fast, slow = fast.next.next, slow.next mid, slow.next = slow.next, None # save and cut. # recursive for cutting. left, right = self.sortList(head), self.sortList(mid) # merge `left` and `right` linked list and return it. h = res = ListNode(0) while left and right: if left.val < right.val: h.next, left = left, left.next else: h.next, right = right, right.next h = h.next h.next = left if left else right return res.next if __name__ == '__main__': nums = [4, 2, 1, 3] head = generateList(nums) printList(head) solution = Solution() printList(solution.sortList(head))
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def reversePrint_1(self, head: ListNode) -> List[int]: return self.reversePrint_1(head.next) + [head.val] if head else [] def reversePrint_2(self, head: ListNode) -> List[int]: stack = list() while head: stack.append(head.val) head = head.next stack.reverse() # return stack return stack[::-1] if __name__ == '__main__': head = generateList([1, 3, 2, 5, 9, 0]) solution = Solution() print(solution.reversePrint_1(head)) printList(head) print(solution.reversePrint_2(head)) printList(head)
:return: """ prehead = ListNode(-1) prev = prehead while l1 and l2: if l1.val <= l2.val: prev.next = l1 l1 = l1.next else: prev.next = l2 l2 = l2.next prev = prev.next # 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可 prev.next = l1 if l1 is not None else l2 return prehead.next if __name__ == '__main__': list_node_1 = generateList([1, 2, 4]) list_node_2 = generateList([1, 3, 4]) solution = Solution() # printList(solution.mergeTwoLists_1(list_node_1, list_node_2)) printList(solution.mergeTwoLists_2(list_node_1, list_node_2))
l1 = self.merge(lists, left, mid) l2 = self.merge(lists, mid + 1, right) return self.mergeTwoLists(l1, l2) def mergeTwoLists(self, l1, l2): if not l1: return l2 if not l2: return l1 if l1.val < l2.val: l1.next = self.mergeTwoLists(l1.next, l2) return l1 else: l2.next = self.mergeTwoLists(l1, l2.next) return l2 if __name__ == '__main__': soltuion = Solution() listNode1 = generateList([1, 4, 5]) listNode2 = generateList([1, 3, 4]) listNode3 = generateList([2, 6]) list_nodes = [listNode1, listNode2, listNode3] mergeList1 = soltuion.mergeKLists(list_nodes) printList(mergeList1) mergeList2 = soltuion.mergeKLists_2(list_nodes) printList(mergeList2)