from common import ListNode class Solution(object): def hasCycle(self, head): if not head: return False p1, p2 = head, head.next.next if head.next else None while p1 and p2 and p2.next and p1 != p2: p1, p2 = p1.next, p2.next.next return p1 == p2 != None if __name__ == '__main__': solution = Solution() l = ListNode.list2ListNode([3, 2, 0, -4]) l.next.next.next.next = l.next print(solution.hasCycle(l))
class Solution: def deleteDuplicates(self, head): pre_p = p = head while p: cur_val, cur_p = p.val, p while p.next and p.next.val == cur_val: p = p.next if cur_p != p: if head == cur_p: head = p.next pre_p = head else: pre_p.next = p.next else: pre_p = p p = p.next return head if __name__ == '__main__': solution = Solution() print( solution.deleteDuplicates(ListNode.list2ListNode([1, 2, 3, 3, 4, 4, 5]))) print(solution.deleteDuplicates(ListNode.list2ListNode([1, 1, 2, 2]))) print(solution.deleteDuplicates(ListNode.list2ListNode([1, 1, 1, 2, 3]))) print( solution.deleteDuplicates( ListNode.list2ListNode([1, 2, 3, 3, 4, 4, 5, 6, 6])))
from common import ListNode class Solution: def removeNthFromEnd(self, head, n): slow_pointer, fast_pointer = head, head while n > 0: fast_pointer = fast_pointer.next n -= 1 if fast_pointer is None: return head.next while fast_pointer.next is not None: slow_pointer = slow_pointer.next fast_pointer = fast_pointer.next slow_pointer.next = slow_pointer.next.next return head if __name__ == '__main__': solution = Solution() print(solution.removeNthFromEnd(ListNode.list2ListNode([1]), 1))
from common import ListNode class Solution: def rotateRight(self, head: ListNode, k): if head is None: return None p = head l = 1 while p.next is not None: l += 1 p = p.next tail = p p = head k = (l - k) % l if k > 0: for i in range(1, k): p = p.next tail.next = head head = p.next p.next = None return head if __name__ == '__main__': solution = Solution() print(solution.rotateRight(ListNode.list2ListNode([1, 2, 3, 4, 5]), 1))
class Solution: def insertionSortList(self, head): p = head while p and p.next: pre_sp = sp = head while p.next.val > sp.val and sp != p.next: pre_sp = sp sp = sp.next if sp != p.next: next_next_node = p.next.next if sp == head: p.next.next = head head = p.next else: pre_sp.next = p.next p.next.next = sp p.next = next_next_node else: p = p.next return head if __name__ == '__main__': solution = Solution() from random import shuffle l=list(range(200)) shuffle(l) print(l) print(solution.insertionSortList(ListNode.list2ListNode(l)))
class Solution: def reverseKGroup(self, head, k): pre_head, cur_head, p, index = None, head, head, 1 while p: if index % k == 0: next_head = p.next if pre_head is None: head = p else: pre_head.next = p pre_head = cur_head pre_tmp_p, tmp_p = cur_head, cur_head.next while pre_tmp_p != p: next_tmp_p = tmp_p.next tmp_p.next = pre_tmp_p pre_tmp_p, tmp_p = tmp_p, next_tmp_p p = cur_head.next = next_head cur_head = next_head else: p = p.next index += 1 return head if __name__ == '__main__': solution = Solution() print( solution.reverseKGroup( ListNode.list2ListNode([1, 7, 3, 2, 7, 0, 1, 0, 0]), 4))
from common import ListNode, TreeNode class Solution: def sortedListToBST(self, head): p = head l = [] while p: l.append(p.val) p = p.next return self.f(l) if head else [] def f(self, l: list): mid = (len(l) - 1) // 2 node = TreeNode(l[mid]) if mid > 0: node.left = self.f(l[:mid]) if mid < len(l) - 1: node.right = self.f(l[mid + 1:]) return node if __name__ == '__main__': solution = Solution() print(solution.sortedListToBST(ListNode.list2ListNode([])))
from common import ListNode class Solution: def numComponents(self, head, G): hsh = {} for idx, val in enumerate(G): hsh[val] = idx id = 0 while head: if head.val in hsh: hsh[head.val] = id else: id += 1 head = head.next return len(set(hsh.values())) if __name__ == '__main__': solution = Solution() print( solution.numComponents(ListNode.list2ListNode([1, 2, 0, 4, 3]), [3, 4, 0, 2, 1]))
from common import ListNode class Solution: def splitListToParts(self, root, k): length, node = 0, root while node: node = node.next length += 1 part_length, add_length_index, index, res, p = length // k, length % k, 0, [], root while index < k: l = part_length + (index < add_length_index) res.append(p) while l > 1 and p: l -= 1 p = p.next if p: p.next, p = None, p.next index += 1 return res if __name__ == '__main__': solution = Solution() print(solution.splitListToParts(ListNode.list2ListNode([1, 2, 3]), 5)) print(solution.splitListToParts(ListNode.list2ListNode([]), 3))
from common import ListNode class Solution: def reorderList(self, head): if not head: return None stack = [] p = head while p: stack.append(p) p = p.next l, p = len(stack), head stack = stack[l - (l // 2):] while len(stack): node = stack.pop() next_node = p.next p.next = node node.next = next_node p = next_node p.next = None if __name__ == '__main__': solution = Solution() l = ListNode.list2ListNode([1, 2, 3]) solution.reorderList(l) print(l)
from common import ListNode class Solution: def __init__(self, head): node = self.head = head self.length = 0 while node: self.length += 1 node = node.next def getRandom(self): if not self.head: return None from random import randint index = randint(0, self.length - 1) node = self.head while index: node = node.next index -= 1 return node.val if __name__ == '__main__': solution = Solution(ListNode.list2ListNode([])) print(solution.getRandom())
index += 1 if p == head and m <= 1: pre_start = None p = head else: pre_start = p p = p.next index += 1 pre_p, p, index = p, p.next, index + 1 while p and index <= n: next_p = p.next p.next = pre_p pre_p = p p = next_p index += 1 if pre_start is None: head.next = p head = pre_p else: pre_start.next.next = p pre_start.next = pre_p return head if __name__ == '__main__': solution = Solution() print(solution.reverseBetween(ListNode.list2ListNode([1]), 1, 1))
class Solution(object): def getIntersectionNode(self, headA, headB): pA, pB, lA, lB = headA, headB, 0, 0 while pA: lA += 1 pA = pA.next while pB: lB += 1 pB = pB.next pA, pB = headA, headB while lA > lB: pA = pA.next lA -= 1 while lB > lA: pB = pB.next lB -= 1 while pA and pB and pA != pB: pA = pA.next pB = pB.next return pA and pB if __name__ == '__main__': solution = Solution() l1 = ListNode.list2ListNode([1, 2, 3]) l2 = ListNode.list2ListNode([1, 2, 3, 4, 5, 6]) l3 = ListNode.list2ListNode([7]) l1.append(l3) l2.append(l3) print(solution.getIntersectionNode(l1, l2))
q = next_q return q[-1] if q else None def merge(self, l1, l2): head, tail, p1, p2 = None, None, l1, l2 while p1 and p2: if p1.val < p2.val: node = p1 p1 = p1.next else: node = p2 p2 = p2.next if head is None and tail is None: head = tail = node else: tail.next = node tail = tail.next if p1: tail.next = p1 if p2: tail.next = p2 return head if __name__ == '__main__': solution = Solution() from random import shuffle l = list(range(200)) # shuffle(l) print(l) print(solution.sortList(ListNode.list2ListNode(l)))
from common import ListNode class Solution: def oddEvenList(self, head): if not head: return None odd, even, even_head = head, head.next, head.next if not even: return head while odd.next and even.next: odd.next = odd.next.next even.next = even.next.next odd, even = odd.next, even.next odd.next = even_head return head if __name__ == '__main__': solution = Solution() print(solution.oddEvenList(ListNode.list2ListNode([]))) print(solution.oddEvenList(ListNode.list2ListNode([1]))) print(solution.oddEvenList(ListNode.list2ListNode([1, 2]))) print(solution.oddEvenList(ListNode.list2ListNode([1, 2, 3]))) print(solution.oddEvenList(ListNode.list2ListNode([1, 2, 3, 4]))) print(solution.oddEvenList(ListNode.list2ListNode([1, 2, 3, 4, 5])))
def addTwoNumbers(self, l1, l2): stack1, stack2 = [], [] while l1: stack1.append(l1.val) l1 = l1.next while l2: stack2.append(l2.val) l2 = l2.next next_node, add_on = None, 0 while stack1 or stack2 or add_on: if stack1 and stack2: val = stack1.pop() + stack2.pop() + add_on elif stack1: val = stack1.pop() + add_on elif stack2: val = stack2.pop() + add_on else: val = add_on node = ListNode(val % 10) add_on = val // 10 node.next = next_node next_node = node return next_node if __name__ == '__main__': solution = Solution() print( solution.addTwoNumbers(ListNode.list2ListNode([0]), ListNode.list2ListNode([5, 6, 4])))
from common import ListNode class Solution: def swapPairs(self, head: ListNode): if head is None or head.next is None: return head pre, post = head, head.next head = post pre_pre = None while post: post_next = post.next post.next = pre pre.next = post_next if pre_pre: pre_pre.next = post pre_pre = pre pre = post_next post = pre.next if pre else None return head if __name__ == '__main__': solution = Solution() print(solution.swapPairs(ListNode.list2ListNode([])))
from common import ListNode class Solution: def mergeTwoLists(self, l1, l2): head, p1, p2 = ListNode(None), l1, l2 p = head while p1 and p2: min_p = min(p1, p2, key=lambda p: p.val if p else float('-inf')) p.next = min_p p = p.next if min_p == p1: p1 = p1.next else: p2 = p2.next min_p.next = None p.next = p1 or p2 return head.next if __name__ == '__main__': solution = Solution() print( solution.mergeTwoLists(ListNode.list2ListNode([]), ListNode.list2ListNode([])))
from common import ListNode class Solution: def mergeKLists(self, lists): head = tail = None while True: min_, index = float('inf'), -1 for i, p in enumerate(lists): if p and p.val < min_: index, min_ = i, p.val if index < 0: break min_node = lists[index] if head is None: head = tail = min_node else: tail.next = min_node tail = min_node lists[index] = min_node.next min_node.next = None return head if __name__ == '__main__': solution = Solution() print(solution.mergeKLists([ListNode.list2ListNode([]), ListNode.list2ListNode([1, 3, 4]), ListNode.list2ListNode([2, 6])]))
if head is None: return None p = head while p.next and p.val < x and p.next.val < x: p = p.next left_tail = p if head.val < x else None pre_p = p p = p.next while p: if p.val < x: pre_p.next = p.next if left_tail is None: p.next = head head = p else: p.next = left_tail.next left_tail.next = p left_tail = p p = pre_p.next else: pre_p = p p = p.next return head if __name__ == '__main__': solution = Solution() print(solution.partition(ListNode.list2ListNode([2, 1]), 2))
from common import ListNode class Solution: def middleNode(self, head): l, p = 0, head while p: l += 1 p = p.next i, p = 0, head while i < l // 2: p = p.next i += 1 return p if __name__ == "__main__": solution = Solution() print(solution.middleNode(ListNode.list2ListNode([1]))) print(solution.middleNode(ListNode.list2ListNode([1, 2, 3, 4, 5]))) print(solution.middleNode(ListNode.list2ListNode([1, 2, 3, 4, 5, 6])))