def func(self, root): pass def oddEvenList(self, head): """ :type head: ListNode :rtype: ListNode """ slow = head fast = head.next.next while slow and fast: slownext = slow.next fastnext = fast.next slow.next = fast fast.next = slownext slownext.next = fastnext slow = slow.next.next fast = fastnext.next return head if __name__ == '__main__': line = "[1,2,3,4,5]" head = leetcode_utils.stringToListNode(line) out = Solution().oddEvenList(head) str = leetcode_utils.listNodeToString(out) print str
""" return self.swap(head) pass def swap(self, node): # 1.确定终止条件 if not node or not node.next: return node # 2.中间态 newHead = self.swap(node.next.next) # 把node.next暂存起来 nextNode = node.next # 移动指针 node.next = newHead nextNode.next = node # 返回值是当前节点的下一个节点 return nextNode if __name__ == '__main__': list = "[1,2,3,4]" head1 = leetcode_utils.stringToListNode(list) result = Solution().swapPairs(head1) resultString = leetcode_utils.listNodeToString(result) print resultString
return pre # 使用栈逆序 def reverseNodeWithStack(self, head): cur = head stack = [] while cur: stack.append(cur) cur = cur.next newhead = stack.pop(-1) cur = newhead while stack: next = stack.pop(-1) cur.next = next cur = cur.next cur.next = None return newhead if __name__ == '__main__': line = "[1,2,3,4,5]" head = leetcode_utils.stringToListNode(line) line = "3" k = leetcode_utils.stringToInt(line) ret = Solution().reverseKGroup(head, k) out = leetcode_utils.listNodeToString(ret) print out
# q.next = q.next.next # return head if not head or not head.next: return None fast = head slow = head for i in range(n): fast = fast.next if not fast: return head.next while fast: fast = fast.next slow = slow.next slow.next = slow.next.next return head if __name__ == '__main__': list1 = "[1,2]" n = 2 # list1 = "[]" # list2 = "[0]" head1 = leetcode_utils.stringToListNode(list1) result = Solution().removeNthFromEnd(head1, 2) print leetcode_utils.listNodeToString(result)
return head def reverse2(self, head, left, right): dummy_node = ListNode(-1) dummy_node.next = head pre = dummy_node for _ in range(left - 1): pre = pre.next cur = pre.next for _ in range(right - left): next = cur.next cur.next = next.next next.next = pre.next pre.next = next return dummy_node.next if __name__ == '__main__': head = "[1, 2, 3, 4, 5,6]" left = 2 right = 5 # head = "[3,5]" # left = 1 # right = 2 link = leetcode_utils.stringToListNode(head) outlink = Solution().reverseBetween(link, left, right) out = leetcode_utils.listNodeToString(outlink) print out
pre.next = cur else: break return newHead.next # 递归思路 def RecursiveDeleteDuplicates(self, head): if not head or not head.next: return head if head.val != head.next.val: head.next = self.deleteDuplicates(head.next) else: move = head.next while move and head.val == move.val: move = move.next return self.deleteDuplicates(move) return head if __name__ == '__main__': headlist = "[1,1,1,2,3]" head = leetcode_utils.stringToListNode(headlist) outHead = Solution().deleteDuplicates(head) outlist = leetcode_utils.listNodeToString(outHead) print outlist
:rtype: ListNode """ small = ListNode(0) smallHead = small large = ListNode(0) largeHead = large while head: if head.val < x: small.next = head small = small.next else: large.next = head large = large.next head = head.next large.next = None small.next = largeHead.next return smallHead.next if __name__ == '__main__': line = "[1,4,3,2,5,2]" head = leetcode_utils.stringToListNode(line) x = 3 out = Solution().partition(head,x) print leetcode_utils.listNodeToString(out)
array = [] cur = head while cur: array.append(cur) cur = cur.next N = len(array) l = 0 r = N - 1 while l <= r: lnode = array[l] rnode = array[r] temp = lnode.next lnode.next = rnode if temp == rnode or lnode == rnode: rnode.next = None else: rnode.next = temp l += 1 r -= 1 return head if __name__ == '__main__': line = "[1,2,3,4,5,6]" head = leetcode_utils.stringToListNode(line) Solution().reorderList(head) out = leetcode_utils.listNodeToString(head) print out