""" p = head less_head = less_tail = None greater_head = greater_tail = None while p: if p.val < x: if less_head is None: less_head = less_tail = p else: less_tail.next = p less_tail = p else: if greater_head is None: greater_head = greater_tail = p else: greater_tail.next = p greater_tail = p p = p.next if greater_tail: greater_tail.next = None if less_tail: less_tail.next = greater_head return less_head or greater_head if __name__ == '__main__': f = Solution().partition from utils import ListNode print f(ListNode.make_list([1, 4, 3, 2, 5, 2]), 3) print f(ListNode.make_list([1]), 0)
class Solution: # @param head, a ListNode # @return a ListNode def deleteDuplicates(self, head): if head is None: return head new_head = head.__class__(0) new_head.next = head pre_last = new_head last = head current = head.next last_delete = None while current: if current.val == last.val: pre_last.next = current.next last_delete = current.val else: if last_delete != last.val: pre_last = last last = current current = current.next return new_head.next if __name__ == "__main__": from utils import ListNode head = ListNode.make_list([1, 1, 2, 2]) p = Solution().deleteDuplicates(head) print p
last = p p = next return last, head, p if k <= 1: return head result = None last = None p = head while p: new_head, new_tail, next = reverse(p, k) if p is head: result = new_head if last: last.next = new_head last = new_tail p = next return result if __name__ == '__main__': from utils import ListNode f = Solution().reverseKGroup print f(ListNode.make_list([1, 2, 3, 4, 5]), 2) print f(ListNode.make_list([1, 2, 3, 4, 5]), 3) print f(ListNode.make_list([1, 2, 3, 4, 5]), 1) print f(ListNode.make_list([1, 2, 3, 4, 5]), 0) print f(ListNode.make_list([1, 2, 3, 4, 5]), 4) print f(ListNode.make_list([1, 2, 3, 4, 5]), 5) print f(ListNode.make_list([1, 2, 3, 4, 5]), 6)
h2 = self.merge_sort(head2, n - n / 2) p1, p2 = h1, h2 h = t = None while p1 or p2: if not p2 or p1 and p1.val <= p2.val: if h is None: h = t = p1 else: t.next = p1 t = p1 p1 = p1.next elif not p1 or p2 and p2.val <= p1.val: if h is None: h = t = p2 else: t.next = p2 t = p2 p2 = p2.next else: break return h if __name__ == '__main__': f = Solution().sortList from utils import ListNode print f(ListNode.make_list([3, 4, 2, 1, 7, 5, 6])) print f(ListNode.make_list([1])) print f(ListNode.make_list([5, 4, 3])) print f(ListNode.make_list([4,19,14,5,-3,1,8,5,11,15]))
last = p p = next return last, head, p if k <= 1: return head result = None last = None p = head while p: new_head, new_tail, next = reverse(p, k) if p is head: result = new_head if last: last.next = new_head last = new_tail p = next return result if __name__ == '__main__': from utils import ListNode f = Solution().reverseKGroup print f(ListNode.make_list([1, 2, 3, 4, 5]), 2) print f(ListNode.make_list([1, 2, 3, 4, 5]), 3) print f(ListNode.make_list([1, 2, 3, 4, 5]), 1) print f(ListNode.make_list([1, 2, 3, 4, 5]), 0) print f(ListNode.make_list([1, 2, 3, 4, 5]), 4) print f(ListNode.make_list([1, 2, 3, 4, 5]), 5) print f(ListNode.make_list([1, 2, 3, 4, 5]), 6)
from utils import ListNode class Solution(object): def oddEvenList(self, head): """ :type head: ListNode :rtype: ListNode """ h1 = t1 = ListNode(None) h2 = t2 = ListNode(None) p = head while p: t1.next = p t1 = p p = p.next if p: t2.next = p t2 = p p = p.next t2.next = None t1.next = h2.next return h1.next if __name__ == '__main__': f = Solution().oddEvenList head = ListNode.make_list([1, 2, 3, 4, 5]) print f(head) print f(None)
r = 0 while p1 or p2: p1_next = p1.next if p1 else None p2_next = p2.next if p2 else None val = (p1.val if p1 else 0) + (p2.val if p2 else 0) + r if val >= 10: val -= 10 r = 1 else: r = 0 node = ListNode(val) if head is None: head = tail = node else: tail.next = node tail = node p1 = p1_next p2 = p2_next if r: node = ListNode(r) tail.next = node return head if __name__ == '__main__': f = Solution().addTwoNumbers print f(ListNode.make_list([2, 4, 3]), ListNode.make_list([5, 6, 4])) print f(ListNode.make_list([2, 4, 3]), ListNode.make_list([5, 6, 9])) print f(ListNode.make_list([2, 4, 3]), None) print f(None, None)
class Solution: # @param head, a ListNode # @return a ListNode def deleteDuplicates(self, head): if head is None: return head new_head = head.__class__(0) new_head.next = head pre_last = new_head last = head current = head.next last_delete = None while current: if current.val == last.val: pre_last.next = current.next last_delete = current.val else: if last_delete != last.val: pre_last = last last = current current = current.next return new_head.next if __name__ == "__main__": from utils import ListNode head = ListNode.make_list([1, 1, 2, 2]) p = Solution().deleteDuplicates(head) print p
class Solution(object): def rotateRight(self, head, k): """ :type head: ListNode :type k: int :rtype: ListNode """ if not head: return head n = 0 p = head tail = p while p: tail = p p = p.next n += 1 tail.next = head k = k % n for i in xrange(n - k): tail = tail.next new_head = tail.next tail.next = None return new_head if __name__ == '__main__': f = Solution().rotateRight from utils import ListNode head = ListNode.make_list([1, 2, 3, 4, 5]) print f(head, 2)
node = p.__class__(p.val) if tail and node.val > tail.val: tail = self.insert(tail, tail, node) else: tail = self.insert(new_head, tail, node) p = p.next return new_head.next def insert(self, head, tail, node): current = head.next last = head while current: if current.val > node.val: break last = current current = current.next if last: last.next = node node.next = current return node if current is None else tail if __name__ == "__main__": from datetime import datetime from utils import ListNode head = ListNode.make_list([3, 2, 1]) st = datetime.now() p = Solution().insertionSortList(head) # print p.to_list() print datetime.now() - st
""" p = head less_head = less_tail = None greater_head = greater_tail = None while p: if p.val < x: if less_head is None: less_head = less_tail = p else: less_tail.next = p less_tail = p else: if greater_head is None: greater_head = greater_tail = p else: greater_tail.next = p greater_tail = p p = p.next if greater_tail: greater_tail.next = None if less_tail: less_tail.next = greater_head return less_head or greater_head if __name__ == '__main__': f = Solution().partition from utils import ListNode print f(ListNode.make_list([1, 4, 3, 2, 5, 2]), 3) print f(ListNode.make_list([1]), 0)
# self.val = x # self.next = None class Solution(object): def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ p = head while p and p.val == val: p = p.next head = p last = None while p: if p.val == val: last.next = p.next else: last = p p = p.next return head if __name__ == '__main__': f = Solution().removeElements from utils import ListNode print f(ListNode.make_list([1, 2, 6, 3, 4, 5, 6]), 6) print f(ListNode.make_list([6, 6, 1, 2, 6, 3, 4, 5, 6]), 6) print f(ListNode.make_list([1, 2, 2, 1]), 2)
@param head The linked list's head. Note that the head is guaranteed to be not null, so it contains at least one node. :type head: ListNode """ self.head = head def getRandom(self): """ Returns a random node's value. :rtype: int """ # Reservoir Sampling p = self.head result = None i = 0 while p: if random.randint(0, i) == 0: result = p.val i += 1 p = p.next return result if __name__ == '__main__': from utils import ListNode s = Solution(ListNode.make_list([1, 2, 3, 4])) print s.getRandom() print s.getRandom() print s.getRandom() print s.getRandom()
:rtype: bool """ slow = fast = head while fast: slow = slow.next fast = fast.next.next if fast.next else None # slow is head of right half now, reverse right half p, last = slow, None while p: next = p.next p.next = last last = p p = next # compare left half and right half p1, p2 = head, last while p1 and p2: if p1.val != p2.val: return False p1 = p1.next p2 = p2.next return True if __name__ == '__main__': from utils import ListNode f = Solution().isPalindrome assert f(ListNode.make_list([1, 2, 3, 2, 1])) assert f(ListNode.make_list([1, 2, 2, 1])) assert not f(ListNode.make_list([1, 2, 1, 1])) assert not f(ListNode.make_list([1, 2, 3, 1, 1]))
node = p.__class__(p.val) if tail and node.val > tail.val: tail = self.insert(tail, tail, node) else: tail = self.insert(new_head, tail, node) p = p.next return new_head.next def insert(self, head, tail, node): current = head.next last = head while current: if current.val > node.val: break last = current current = current.next if last: last.next = node node.next = current return node if current is None else tail if __name__ == "__main__": from datetime import datetime from utils import ListNode head = ListNode.make_list([3, 2, 1]) st = datetime.now() p = Solution().insertionSortList(head) # print p.to_list() print datetime.now() - st
i = 1 p = head last = None while i < m: last = p p = p.next i += 1 rhead = p rlast = None while p and i <= n: next = p.next p.next = rlast rlast = p p = next i += 1 rhead.next = p if m > 1: last.next = rlast else: head = rlast return head if __name__ == '__main__': from utils import ListNode f = Solution().reverseBetween print f(ListNode.make_list([1, 2, 3, 4, 5]), 2, 4) print f(ListNode.make_list([1, 2, 3, 4, 5]), 2, 5) print f(ListNode.make_list([1, 2, 3, 4, 5]), 1, 5) print f(ListNode.make_list([1, 2, 3, 4, 5]), 1, 4)