import sys sys.path = sys.path = ['.', '../', '../../'] + sys.path from util import ListNode, initList, printList class Solution: def insertionSortList(self, head: ListNode) -> ListNode: sortedList = None while head: ptr, head = head, head.next prev, curr = None, sortedList while curr and ptr.val > curr.val: prev, curr = curr, curr.next if prev: ptr.next = prev.next prev.next = ptr elif curr: ptr.next = sortedList sortedList = ptr else: ptr.next = None sortedList = ptr return sortedList # 1 -> 2 -> 3 -> 4 -> NULL printList(Solution().insertionSortList(initList([4, 2, 1, 3]))) # -1 -> 0 -> 3 -> 4 -> 5 -> NULL printList(Solution().insertionSortList(initList([-1, 5, 3, 4, 0])))
mid.next = reversed_tail reversed_tail = mid mid = nxt # Merge the first half and the reversed second half of the list pre, curr = None, head while curr and reversed_tail: nxt_front, nxt_tail = curr.next, reversed_tail.next pre = curr.next = reversed_tail reversed_tail.next = nxt_front curr, reversed_tail = nxt_front, nxt_tail if reversed_tail: pre.next = reversed_tail llst = initList([1, 2, 3, 4]) Solution().reorderList(llst) printList(llst) llst = initList([1, 2, 3, 4, 5]) Solution().reorderList(llst) printList(llst) llst = initList([1]) Solution().reorderList(llst) printList(llst) llst = initList([1, 2]) Solution().reorderList(llst) printList(llst)
# node1 = curr # curr, length = curr.next, length + 1 # node2 = head # for _ in range(length - k): # node2 = node2.next # node1.val, node2.val = node2.val, node1.val # return head # O(n) time & O(n) space lst, curr, idx = [], head, 1 while curr: lst+= [curr] curr, idx = curr.next, idx + 1 lst[k - 1].val, lst[-k].val = lst[-k].val, lst[k - 1].val return head # 5 -> 2 -> 3 -> 4 -> 1 -> NULL printList(Solution().swapNodes(initList([1, 2, 3, 4, 5]), 1)) # 1 -> 4 -> 3 -> 2 -> 5 -> NULL printList(Solution().swapNodes(initList([1, 2, 3, 4, 5]), 2)) # 1 -> 2 -> 3 -> 4 -> 5 -> NULL printList(Solution().swapNodes(initList([1, 2, 3, 4, 5]), 3)) # 1 -> 4 -> 3 -> 2 -> 5 -> NULL printList(Solution().swapNodes(initList([1, 2, 3, 4, 5]), 4)) # 5 -> 2 -> 3 -> 4 -> 1 -> NULL printList(Solution().swapNodes(initList([1, 2, 3, 4, 5]), 5))
if llst1: tail.next = llst1 elif llst2: tail.next = llst2 return head def mergeSort(head): if not head or not head.next: return head llst1, llst2, midPrev = head, head, None while llst1 and llst1.next: midPrev = midPrev.next if midPrev else llst1 llst1 = llst1.next.next midPrev.next, llst2, llst1 = None, midPrev.next, head llst1, llst2 = mergeSort(llst1), mergeSort(llst2) llst = mergeList(llst1, llst2) return llst return mergeSort(head) # 1 -> 2 -> 3 -> 4 -> NULL printList(Solution().sortList(initList([4, 2, 1, 3]))) # -1 -> 0 -> 3 -> 4 -> 5 -> NULL printList(Solution().sortList(initList([-1, 5, 3, 4, 0]))) # NULL printList(Solution().sortList(initList([])))
length, ptr = length + 1, ptr.next # Calculate the offset offset, prev, ptr = k % length, None, head if offset == 0: # Remains unchanged return head # Find the head of the new linked list for i in range(length - offset): prev, ptr = ptr, ptr.next newHead = ptr # Link the original tail to original head # Update new tail tail.next, prev.next = head, None return newHead # 4 -> 5 -> 1 -> 2 -> 3 printList(Solution().rotateRight(initList([1, 2, 3, 4, 5]), 2)) # 2 -> 0 -> 1 printList(Solution().rotateRight(initList([0, 1, 2]), 4)) # None printList(Solution().rotateRight(initList([]), 0)) # 1 printList(Solution().rotateRight(initList([1]), 1))
class Solution: def isPalindrome(self, head: ListNode) -> bool: # O(n) time & O(1) space # Find the mid-point of the linked list curr, mid = head, head while curr and curr.next: curr, mid = curr.next.next, mid.next # Reverse the second half of the linked list reversed_h = None while mid: nxt = mid.next mid.next = reversed_h reversed_h = mid mid = nxt # Compare the first and the reversed second half while reversed_h: if reversed_h.val != head.val: return False head, reversed_h = head.next, reversed_h.next return True # True print(Solution().isPalindrome(initList([1, 2, 2, 1]))) # False print(Solution().isPalindrome(initList([1, 2])))
# self.next = next import sys sys.path = sys.path = ['.', '../', '../../'] + sys.path from util import ListNode, initList class Solution: def getDecimalValue(self, head: ListNode) -> int: ans = 0 while head: ans = (ans << 1) + head.val head = head.next return ans # 5 print(Solution().getDecimalValue(initList([1, 0, 1]))) # 0 print(Solution().getDecimalValue(initList([0]))) # 1 print(Solution().getDecimalValue(initList([1]))) # 18880 print(Solution().getDecimalValue(initList([1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0]))) # 0 print(Solution().getDecimalValue(initList([0, 0])))
sys.path = ['.', '../', '../../'] + sys.path from util import ListNode, initList, printList class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: prev_target, target, curr, diff = None, head, head, 1 while diff < n and curr: curr, diff = curr.next, diff + 1 while curr and curr.next: prev_target, target, curr = target, target.next, curr.next if prev_target: prev_target.next = target.next elif target: head = head.next else: head = None return head # 1 -> 2 -> 3 -> 5 -> NULL printList(Solution().removeNthFromEnd(initList([1, 2, 3, 4, 5]), 2)) # NULL printList(Solution().removeNthFromEnd(initList([1]), 1)) # 1 -> NULL printList(Solution().removeNthFromEnd(initList([1, 2]), 1)) # 2 -> NULL printList(Solution().removeNthFromEnd(initList([1, 2]), 2))
""" from util import ListNode, initList, printList # Definition for singly-linked list. # class ListNode: # def __init__(self, val = 0, next = None): # self.val = val # self.next = next class Solution: def removeElements(self, head: ListNode, val: int) -> ListNode: prev, curr = None, head while curr: if curr.val == val: if curr == head: head = head.next prev, curr = None, head continue prev.next = curr.next curr = curr.next continue prev, curr = curr, curr.next return head printList(Solution().removeElements(initList([1, 2, 6, 3, 4, 5, 6]), 6)) # 1 -> 2 -> 3 -> 4 -> 5 printList(Solution().removeElements(initList([6, 6, 6]), 6)) # (None) printList(Solution().removeElements(initList([1]), 1)) # (None) printList(Solution().removeElements(initList([1, 2, 2, 1]), 2)) # 1 -> 1
# self.next = next class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: carry, head, tail = 0, None, None while l1 or l2 or carry: d1, d2 = l1.val if l1 else 0, l2.val if l2 else 0 if not head: head = ListNode((d1 + d2 + carry) % 10) tail = head else: tail.next = ListNode((d1 + d2 + carry) % 10) tail = tail.next carry = (d1 + d2 + carry) // 10 l1, l2 = l1.next if l1 else l1, l2.next if l2 else l2 return head # 0 -> 1 -> NULL ll1, ll2 = initList([5]), initList([5]) printList(Solution().addTwoNumbers(ll1, ll2)) # 1 -> 8 -> NULL ll1, ll2 = initList([1, 8]), initList([0]) printList(Solution().addTwoNumbers(ll1, ll2)) # 0 -> 0 -> 0 -> 1 -> NULL ll1, ll2 = initList([9, 9, 9]), initList([1]) printList(Solution().addTwoNumbers(ll1, ll2))
sys.path = ['.', '../', '../../'] + sys.path from util import ListNode, initList, printList class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: prev, curr = None, head while curr and curr.next: if curr.val == curr.next.val: val = curr.val while curr and curr.val == val: if prev: prev.next = curr.next else: head = curr.next curr = curr.next else: prev, curr = curr, curr.next return head # 2 -> 3 -> NULL printList(Solution().deleteDuplicates(initList([1, 1, 1, 2, 3]))) # NULL printList(Solution().deleteDuplicates(initList([1, 1, 1]))) # 1 -> NULL printList(Solution().deleteDuplicates(initList([1])))
# # O(n) time & O(n) space # queue, ptr = [], head # while ptr: # queue.append(ptr) # ptr = ptr.next # while queue: # node = queue.pop(0) # node.next, ptr = ptr, node # return ptr # # O(n) Recursive # if not head or not head.next: # return head # tail = self.reverseList(head.next) # head.next.next, head.next = head, None # return tail # 5 -> 4 -> 3 -> 2 -> 1 -> NULL printList(Solution().reverseList(initList([1, 2, 3, 4, 5]))) # NULL printList(Solution().reverseList(initList([]))) # 1 -> NULL printList(Solution().reverseList(initList([1]))) # 2 -> 1 -> NULL printList(Solution().reverseList(initList([1, 2])))
while l1 and l2: if l1.val < l2.val: if not tail: tail, l1 = l1, l1.next head = tail else: tail.next, l1 = l1, l1.next tail = tail.next else: if not tail: tail, l2 = l2, l2.next head = tail else: tail.next, l2 = l2, l2.next tail = tail.next if l1: tail.next = l1 elif l2: tail.next = l2 return head # 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> NULL printList(Solution().mergeTwoLists(initList([1, 2, 4]), initList([1, 3, 4]))) # NULL printList(Solution().mergeTwoLists(initList([]), initList([]))) # 0 -> NULL printList(Solution().mergeTwoLists(initList([]), initList([0])))
return head # # O(n) time & O(n) space # head, stack1, stack2, carry = None, [], [], 0 # while l1 or l2: # stack1 += [l1.val] if l1 else [] # stack2 += [l2.val] if l2 else [] # l1 = l1.next if l1 else l1 # l2 = l2.next if l2 else l2 # while stack1 or stack2: # num = carry # num += stack1.pop() if stack1 else 0 # num += stack2.pop() if stack2 else 0 # head, carry = ListNode(num % 10, head), num // 10 # head = ListNode(carry, head) if carry else head # return head # 7 -> 8 -> 0 -> 7 -> NULL printList(Solution().addTwoNumbers(initList([7, 2, 4, 3]), initList([5, 6, 4]))) # 1 -> 0 -> NULL printList(Solution().addTwoNumbers(initList([5]), initList([5]))) # 5 -> NULL printList(Solution().addTwoNumbers(initList([]), initList([5]))) # NULL printList(Solution().addTwoNumbers(initList([]), initList([])))