def solution(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None: return None listLen = 1 curp = head while curp.next: listLen += 1 curp = curp.next if listLen == 1: return head else: dummy = ListNode(0) dummy.next = head halfWindow = 1 while halfWindow < listLen: hp = dummy for i in range((listLen - 1) / (halfWindow << 1) + 1): cp = hp for j in range(halfWindow << 1): if i == (listLen - 1) / (halfWindow << 1) and i * ( halfWindow << 1) + j >= listLen: if j <= halfWindow - 1: mp = cp tp = cp else: cp = cp.next if j == halfWindow - 1: mp = cp else: tp = cp if mp == tp: break else: while hp != mp and hp != tp: if hp.next.val <= mp.next.val: hp = hp.next else: minp = mp.next mp.next = minp.next minp.next = hp.next hp.next = minp hp = hp.next if hp == mp: hp = tp elif hp == tp: hp = mp halfWindow <<= 1 return dummy.next
def solution(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None: return None listLen = 1 curp = head while curp.next: listLen += 1 curp = curp.next if listLen == 1: return head else: dummy = ListNode(0) dummy.next = head halfWindow = 1 while halfWindow < listLen: hp = dummy for i in range((listLen-1) / (halfWindow<<1) + 1): cp = hp for j in range(halfWindow<<1): if i == (listLen-1) / (halfWindow<<1) and i*(halfWindow<<1)+j >= listLen: if j <= halfWindow - 1: mp = cp tp = cp else: cp = cp.next if j == halfWindow - 1: mp = cp else: tp = cp if mp == tp: break else: while hp != mp and hp != tp: if hp.next.val <= mp.next.val: hp = hp.next else: minp = mp.next mp.next = minp.next minp.next = hp.next hp.next = minp hp = hp.next if hp == mp: hp = tp elif hp == tp: hp = mp halfWindow <<= 1 return dummy.next
def solution(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head = ListNode(0) curp = head c3 = 0 while l1 and l2: cursum = c3 + l1.val + l2.val if cursum >= 10: c3 = 1 cursum -= 10 else: c3 = 0 curp.next = ListNode(cursum) curp = curp.next l1 = l1.next l2 = l2.next while l1: cursum = c3 + l1.val if cursum >= 10: c3 = 1 cursum -= 10 else: c3 = 0 curp.next = ListNode(cursum) curp = curp.next l1 = l1.next while l2: cursum = c3 + l2.val if cursum >= 10: c3 = 1 cursum -= 10 else: c3 = 0 curp.next = ListNode(cursum) curp = curp.next l2 = l2.next if c3 == 1: curp.next = ListNode(1) return head.next
def solution(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None: return None dummy = ListNode(head.val - 1) dummy.next = head last = dummy curr = head while curr != None and curr.next != None: if curr.val != curr.next.val: last = last.next curr = curr.next else: while curr.val == curr.next.val: curr = curr.next if curr.next == None: break curr = curr.next last.next = curr return dummy.next
def solution(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ if m == n: return head dummy = ListNode(0) dummy.next = head p1 = dummy for i in range(m-1): p1 = p1.next pf = p1.next for j in range(n-m): t = p1.next p1.next = pf.next pf.next = pf.next.next p1.next.next = t return dummy.next
def solution(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None: return None dummy = ListNode(head.val-1) dummy.next = head last = dummy curr = head while curr != None and curr.next != None: if curr.val != curr.next.val: last = last.next curr = curr.next else: while curr.val == curr.next.val: curr = curr.next if curr.next == None: break curr = curr.next last.next = curr return dummy.next
from common.TreeNode import TreeNode, genTree # Valid Binary Search Tree from ValidateBST import IsValidBST ivb = IsValidBST() root = genTree([1, 2, 3, 4, 5, 6, 7], [3, 1, 5, 0, 2, 4, 6]) print ivb.solutions(root) elif titleNum == 103: from BinaryTreeZigzagLevelOrderTraversal import ZigzagLevelOrder # Binary Tree Zigzag Level Order Traversal elif titleNum == 105: from ConstructBinaryTreeFromPreorderAndInorderTraversal import BuildTree # Hard, learn how to save memory during recursive calls elif titleNum == 108: from SortedArrayToBST import SortedArrayToBST elif titleNum == 109: from common.ListNode import ListNode from common.TreeNode import TreeNode from SortedListToBST import SortedListToBST # Convert sorted List to BST, Hard ln = ListNode(1) ln.next = ListNode(3) sltb = SortedListToBST() root = sltb.solution(ln) root.printTreeNodeVal() elif titleNum == 113: from PathSumII import PathSumII # Path Sum II elif titleNum == 120: from Triangle import MinimumTotal # Triangle, find the minimum path of sum elif titleNum == 121: from BestTimeBuySellStock import MaxProfit # best time to buy and sell stock elif titleNum == 127: from WordLadder import LadderLength # Word Ladder, Hard, Very, Smart/Fast way to find words with 1 bit flip elif titleNum == 130: from SurroundedRegions import FlipSurroundedRegions # Surrounded Regions, Hard, BFS loop vs. DFS recursive fsr = FlipSurroundedRegions()