while curr: if step + k + 1 == n: # break prev = curr curr = curr.next step += 1 tail = curr while tail.next: tail = tail.next prev.next = None tail.next = head return curr def _listLength(self, head): n = 0 while head: n += 1 head = head.next return n if __name__ == '__main__': # import doctest # doctest.testmod() head = ListNode.FromList(['a', 'b', 'c']) s = Solution() head = s.rotateRight(head, 1) print head
if head: partitions.append(head) return partitions def reverseList(self, head): if not head: return head if not head.next: return head tail = head curr = head.next head = tail tail.next = None while curr.next: next = curr.next curr.next = head head = curr curr = next return curr if __name__ == '__main__': solution = Solution() partitions = solution.partitionList(ListNode.FromList([1, 2, 3, 4, 5]), 2) print partitions[0] print partitions[1] print partitions[2] print solution.reverseList(ListNode.FromList([1, 2, 3, 4, 5])) # import doctest # doctest.testmod()