from data_structure import ListNode from data_structure import build_link_list from data_structure import ds_print class Solution: def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ p = result = ListNode(0) while l1 is not None and l2 is not None: if l1.val < l2.val: p.next = l1 l1 = l1.next else: p.next = l2 l2 = l2.next p = p.next if l1 is not None: p.next = l1 elif l2 is not None: p.next = l2 return result.next if __name__ == "__main__": ds_print(Solution().mergeTwoLists(build_link_list([1, 2, 4]), build_link_list([1, 3, 4])))
head2 = p.next p.next = None p, q, r = None, head2, head2.next if head2 is not None else None while q is not None: q.next = p p = q q = r if r is not None: r = r.next head2 = p p = head while head2 is not None: q = p.next p.next = head2 head2 = head2.next p.next.next = q p = q if __name__ == "__main__": head = build_link_list([1,2,3,4]) Solution().reorderList(head) ds_print(head) head = build_link_list([1,2,3,4,5]) Solution().reorderList(head) ds_print(head)
from data_structure import build_link_list, ListNode, ds_print class Solution: def partition(self, head: ListNode, x: int) -> ListNode: p0, p1 = ListNode(0), ListNode(0) head0, head1 = p0, p1 while head is not None: if head.val < x: p0.next = head p0 = p0.next else: p1.next = head p1 = p1.next head = head.next p0.next = head1.next p1.next = None return head0.next if __name__ == "__main__": ds_print(Solution().partition(build_link_list([1, 4, 3, 2, 5, 2]), 3))
from data_structure import ListNode, build_link_list class Solution: def isPalindrome(self, head): """ :type head: ListNode :rtype: bool """ nums = [] while head is not None: nums.append(head.val) head = head.next for i in range(len(nums) // 2): if nums[i] != nums[len(nums) - 1 - i]: return False return True if __name__ == "__main__": print(Solution().isPalindrome(build_link_list([1, 2]))) print(Solution().isPalindrome(build_link_list([1, 2, 2, 1])))
p = head # head = ListNode(-1) # head.next = p lh = 0 while p is not None: p = p.next lh += 1 if lh == 0: return [] k %= lh k = lh - k - 1 p = head for _ in range(k): p = p.next q = p while q.next is not None: q = q.next q.next = head head = p.next p.next = None return head if __name__ == "__main__": ds_print(Solution().rotateRight(build_link_list([1, 2, 3, 4, 5]), 2)) ds_print(Solution().rotateRight(build_link_list([0, 1, 2]), 4)) ds_print(Solution().rotateRight(build_link_list([0, 1, 2]), 0)) ds_print(Solution().rotateRight(build_link_list([0]), 4)) ds_print(Solution().rotateRight(build_link_list([0]), 0)) ds_print(Solution().rotateRight(build_link_list([]), 0))
return q, h else: return None, None lr, p = 0, root while p is not None: p, lr = p.next, lr + 1 n, r = lr // k, lr % k result = [] for i in range(k): l = n if i < r: l += 1 root, head = split(root, l) result.append(head) return result if __name__ == "__main__": inputs = [ # ([1, 2, 3], 5), ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3), ] for root_list, k in inputs: root = build_link_list(root_list) result = [ flatten_link_list(head) for head in Solution().splitListToParts(root, k) ] print(result)
from data_structure import ListNode, build_link_list, ds_print class Solution: def deleteDuplicates(self, head: 'ListNode') -> 'ListNode': p = head while p is not None and p.next is not None: while p.next is not None and p.next.val == p.val: p.next = p.next.next p = p.next return head if __name__ == "__main__": ds_print(Solution().deleteDuplicates(build_link_list([1, 1, 2]))) ds_print(Solution().deleteDuplicates(build_link_list([1, 1, 2, 3, 3])))
from data_structure import ListNode, build_link_list, ds_print class Solution: def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ p = head head = ListNode(0) head.next = p p = head while p is not None and p.next is not None: while p.next is not None and p.next.val == val: p.next = p.next.next p = p.next return head.next if __name__ == "__main__": ds_print(Solution().removeElements(build_link_list([1, 2, 6, 3, 4, 5, 6]), 6)) ds_print(Solution().removeElements(build_link_list([1, 1]), 1))
# reverse p = head head = ListNode(0) head.next = p prev_end = head for i in range(0, ll // k): cur_start = cur_end = prev_end.next for j in range(0, k - 1): cur_end = cur_end.next if cur_end is None: next_start = None else: next_start = cur_end.next p, q, r = prev_end, prev_end.next, prev_end.next.next for j in range(0, k): q.next = p p, q = q, r if r is not None: r = r.next cur_start.next = next_start prev_end.next = cur_end prev_end = cur_start return head.next if __name__ == "__main__": ds_print(Solution().reverseKGroup(build_link_list([1, 2, 3, 4, 5]), 2)) ds_print(Solution().reverseKGroup(build_link_list([1, 2, 3, 4, 5]), 3)) ds_print(Solution().reverseKGroup(build_link_list([1, 2, 3, 4, 5, 6]), 3)) ds_print(Solution().reverseKGroup(build_link_list([1, 2, 3, 4, 5]), 1))
from data_structure import ListNode, build_link_list, ds_print class Solution: def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None or head.next is None: return head p, q, r = head, head.next, head.next.next while q is not None: q.next = p p, q = q, r if r is not None: r = r.next head.next = None return p if __name__ == "__main__": ds_print(Solution().reverseList(build_link_list([1, 2, 3, 4, 5]))) ds_print(Solution().reverseList(build_link_list([1, 2]))) ds_print(Solution().reverseList(build_link_list([1]))) ds_print(Solution().reverseList(build_link_list([])))
from data_structure import build_link_list from data_structure import ds_print class Solution: def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ # get length p = head ll = 0 while p is not None: ll += 1 p = p.next on = ll + 1 - n # delete node p = ListNode(0) p.next = head q = p for _ in range(1, on): q = q.next q.next = q.next.next return p.next if __name__ == "__main__": ds_print(Solution().removeNthFromEnd(build_link_list([1, 2, 3, 4, 5]), 2))
class Solution: def sortedListToBST(self, head): """ :type head: ListNode :rtype: TreeNode """ nums = [] p = head while p is not None: nums.append(p.val) p = p.next return self.sortedArrayToBST(nums) def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode """ if len(nums) == 0: return None mid_index = len(nums) >> 1 node = TreeNode(nums[mid_index]) node.left = self.sortedArrayToBST(nums[:mid_index]) node.right = self.sortedArrayToBST(nums[mid_index + 1:]) return node if __name__ == "__main__": ds_print(Solution().sortedListToBST(build_link_list([-10, -3, 0, 5, 9])))
from data_structure import ListNode, build_link_list, ds_print class Solution: def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ p = head head = ListNode(0) head.next = p p = head while p.next is not None and p.next.next is not None: tp1, tp2, q = p.next, p.next.next, p.next.next.next p.next, tp2.next, tp1.next = tp2, tp1, q p = tp1 return head.next if __name__ == "__main__": ds_print(Solution().swapPairs(build_link_list([1, 2, 3, 4]))) ds_print(Solution().swapPairs(build_link_list([1, 2, 3, 4, 5])))
return head head, p = ListNode(0), head head.next = p p, q, r = head, head.next, head.next.next n -= m m -= 1 while m > 0: p, q = q, r if r is not None: r = r.next m -= 1 flag1, flag2 = p, q n += 1 while n > 0: q.next = p n -= 1 if n == 0: flag1.next, flag2.next = q, r break p, q = q, r if r is not None: r = r.next return head.next if __name__ == "__main__": ds_print(Solution().reverseBetween(build_link_list([1, 2, 3, 4, 5]), 2, 4)) ds_print(Solution().reverseBetween(build_link_list([1, 2, 3, 4, 5]), 4, 5)) ds_print(Solution().reverseBetween(build_link_list([1, 2, 3, 4, 5]), 1, 2)) ds_print(Solution().reverseBetween(build_link_list([1, 2, 3, 4, 5]), 1, 1))
class Solution: def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ heap = PriorityQueue() for list_item in lists: if list_item is not None: heap.put(HeapItem(list_item.val, list_item)) p = result_head = ListNode(0) while not heap.empty(): current = heap.get() p.next = current.data if current.data.next is not None: heap.put(HeapItem(current.data.next.val, current.data.next)) p = p.next p.next = None return result_head.next if __name__ == "__main__": # ds_print(Solution().mergeKLists([ # build_link_list([1, 4, 5]), # build_link_list([1, 3, 4]), # build_link_list([2, 6]) # ])) ds_print(Solution().mergeKLists([build_link_list([])]))
from data_structure import ListNode, build_link_list, ds_print class Solution: def insertionSortList(self, head: ListNode) -> ListNode: if head is None: return head sorted_head = head head = head.next sorted_head.next = None while head is not None: current = head head = head.next current.next = None if current.val <= sorted_head.val: current.next = sorted_head sorted_head = current else: p = sorted_head while p.next is not None and current.val > p.next.val: p = p.next current.next = p.next p.next = current return sorted_head if __name__ == "__main__": ds_print(Solution().insertionSortList(build_link_list([4, 2, 1, 3]))) ds_print(Solution().insertionSortList(build_link_list([-1, 5, 3, 4, 0])))