def plusOne(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None: return None dummy = ListNode(0) dummy.next = head nine = dummy while head: if head.val != 9: nine = head head = head.next node = nine.next while node: node.val = 0 node = node.next nine.val += 1 return dummy if dummy.val == 1 else dummy.next
def removeZeroSumSublists(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None: return None dummy = ListNode(0) dummy.next = head diff = {} sum = 0 node = dummy while node: sum += node.val diff[sum] = node node = node.next sum = 0 node = dummy while node: sum += node.val node.next = diff[sum].next node = node.next return dummy.next
def partition(self, head, x): """ :type head: ListNode :type x: int :rtype: ListNode """ if head is None or head.next is None: return head large = ListNode(0) l_node = large small = ListNode(0) s_node = small node = head while node: if node.val < x: s_node.next = node s_node = s_node.next else: l_node.next = node l_node = l_node.next node = node.next l_node.next = None s_node.next = large.next return small.next
def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ if lists is None: return None if len(lists) == 0: return None if len(lists) == 1: return lists[0] from queue import PriorityQueue pq = PriorityQueue() for node in lists: pq.put(node) dummy = ListNode(0) cur = dummy while not pq.empty(): node = pq.get() if node.next: pq.put(node.next) cur.next = node cur = cur.next return dummy.next
def sortList(self, head): """ :type head: ListNode :rtype: ListNode """ node = head length = 0 while node: node = node.next length += 1 if length == 0: return dummy = ListNode(0) dummy.next = head interval = 1 while interval < length: prev = dummy cur = prev.next while cur: first = cur for j in range(interval - 1): if cur: cur = cur.next if cur is None: break second = cur.next cur.next = None cur = second for j in range(interval - 1): if cur: cur = cur.next rest = None if cur: rest = cur.next cur.next = None cur = rest prev.next = self.merge(first, second) while prev.next: prev = prev.next prev.next = rest interval *= 2 return dummy.next
def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None or head.next is None: return head dummy = ListNode(0) dummy.next = head prev, cur = dummy, head while cur: if cur.next and cur.val == cur.next.val: cur = self.findSame(prev, cur.val) else: cur = cur.next prev = prev.next return dummy.next
def reverseBetween(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ if head is None or m == n: return head dummy = ListNode(0) dummy.next = head node = dummy count = 0 while node and count != m - 1: node = node.next count += 1 print(node.val) self.reverse(node, n - m) return dummy.next
def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ extra = 0 dummy = ListNode(0) head = dummy while l1 or l2: if l1: n1 = l1.val else: n1 = 0 if l2: n2 = l2.val else: n2 = 0 res = (n1 + n2 + extra) % 10 extra = (n1 + n2 + extra) // 10 dummy.next = ListNode(res) if l1: l1 = l1.next if l2: l2 = l2.next dummy = dummy.next if extra != 0: dummy.next = ListNode(extra) return head.next
def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None or head.next is None: return head dummy = ListNode(0) dummy.next = head prev = dummy count = 0 node = prev.next while node: count += 1 if count % 2 == 0: prev = self.swap(prev, node) node = prev.next else: node = node.next show_linkedlist(dummy.next)
def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ if head is None: return None if head.next is None and n == 1: return None count = 0 dummy = ListNode(0) dummy.next = head node, cur = dummy, dummy while node: if count > n: cur = cur.next node = node.next count += 1 cur.next = cur.next.next return dummy.next
def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if l1 is None: return l2 if l2 is None: return l1 stack1 = [] stack2 = [] while l1 or l2: if l1: stack1.append(l1.val) l1 = l1.next if l2: stack2.append(l2.val) l2 = l2.next dummy = ListNode(0) extra = 0 while stack1 or stack2: n1 = stack1.pop() if stack1 else 0 n2 = stack2.pop() if stack2 else 0 sum = n1 + n2 + extra node = ListNode(sum % 10) extra = sum // 10 node.next = dummy.next dummy.next = node if extra != 0: node = ListNode(extra) node.next = dummy.next dummy.next = node return dummy.next
def reverseKGroup(self, head, k): """ :type head: ListNode :type k: int :rtype: ListNode """ stack = [] dummy = ListNode(0) new_head = dummy node = head while node: stack.append(node) node = node.next if len(stack) == k: for i in range(k): poped = stack.pop() poped.next = None dummy.next = poped dummy = dummy.next if len(stack) != 0: dummy.next = stack.pop(0) return new_head.next
def merge(self, l1, l2): head = ListNode(0) node = head while l1 and l2: if l1.val < l2.val: node.next = l1 l1 = l1.next else: node.next = l2 l2 = l2.next node = node.next if l1: node.next = l1 if l2: node.next = l2 return head.next
def insertionSortList(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None or head.next is None: return head dummy = ListNode(-0) cur = dummy node = head while node: if cur.next is None or cur.next.val > node.val: prev = node.next node.next = cur.next cur.next = node cur = dummy node = prev else: cur = cur.next return dummy.next