def construcrt_list(self, s: str): prev_node = None for digit in s: num = int(digit) node = ListNode(num, prev_node) prev_node = node return prev_node
def reverseList(self, head: ListNode) -> ListNode: new_head = None current = head while True: if not current: break new_head = ListNode(current.val, new_head) current = current.next return new_head
def swapPairs(self, head: ListNode) -> ListNode: if not head: return head next = head.next if not next: return head rest = next.next next.next = head head.next = self.swapPairs(rest) return next
def list_partition(node: ListNode, value: int) -> ListNode: less = None bigger = None while node: next = node.next if node.val < value: node.next = less less = node else: node.next = bigger bigger = node node = next if not less: return bigger head = less while less.next: less = less.next less.next = bigger return head
def run(): lists = [ ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5, ListNode(6)))))), #ListNode(5, ListNode(2, ListNode(3, ListNode(4, ListNode(1))))), #ListNode(1, ListNode(2, ListNode(3, ListNode(3, ListNode(2, ListNode(1)))))), #ListNode(1, ListNode(1)), ] for l in lists: print(Solution.list_partition(l, 3))
class Solution: def removeElement(self, head: ListNode, n: int) -> ListNode: previous = None current = head if head.val == n: return head.next while current: if current.val == n: previous.next = current.next break previous = current current = current.next return head s = Solution() a5 = ListNode(5) a4 = ListNode(4, a5) a3 = ListNode(3, a4) a2 = ListNode(2, a3) a1 = ListNode(1, a2) b1 = ListNode(1) print(s.removeElement(b1, 1))
def reverseNode(self, current, new): if not current: return new new = ListNode(current.val, new) return self.reverseNode(current.next, new)
break return res def reversed_num(self, s: str): reversed = s[::-1] return int(reversed) def construcrt_list(self, s: str): prev_node = None for digit in s: num = int(digit) node = ListNode(num, prev_node) prev_node = node return prev_node solution = Solution() a1 = ListNode(3) a2 = ListNode(4, a1) a3 = ListNode(2, a2) b1 = ListNode(4) b2 = ListNode(6, b1) b3 = ListNode(5, b2) # 7, 0, 8 print(solution.addTwoNumbers(a3, b3))
def run(): test_cases = [ ListNode( 1, ListNode(2, ListNode(3, ListNode(4, ListNode(5, ListNode(6)))))), ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5))))), ListNode( 1, ListNode(2, ListNode(3, ListNode(3, ListNode(2, ListNode(1)))))), ListNode(1, ListNode(1)), ] for test_case in test_cases: print(Solution.is_palyndrome(test_case))
# https://leetcode.com/problems/swap-nodes-in-pairs/ from tasks.utils.list_node import ListNode class Solution: def swapPairs(self, head: ListNode) -> ListNode: if not head: return head next = head.next if not next: return head rest = next.next next.next = head head.next = self.swapPairs(rest) return next n8 = ListNode(8) n7 = ListNode(7, n8) n6 = ListNode(6, n7) n5 = ListNode(5, n6) n4 = ListNode(4, n5) n3 = ListNode(3, n4) n2 = ListNode(2, n3) n1 = ListNode(1, n2) s = Solution() print(s.swapPairs(n1))
for i in range(len(heads)): if heads[i].val < current_min: target_i = i current_min = heads[i].val target = heads[target_i] if head is None: head = target cur = head else: cur.next = target cur = target if target.next: heads[target_i] = target.next else: heads.pop(target_i) return head s = Solution() print( s.mergeKLists([ ListNode(50, ListNode(60, ListNode(70))), ListNode(1, ListNode(4, ListNode(6))), ListNode(2, ListNode(88, ListNode(101))), ])) print(s.mergeKLists([])) print(s.mergeKLists([None, None]))