def reverseBetween(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ if not head or m >= n: return head dummy = ListNode(0) dummy.next = head head = dummy for i in xrange(1, m): if not head: return None head = head.next last = head.next for i in xrange(m, n): if not head: return None tmp = last.next last.next = tmp.next tmp.next = head.next head.next = tmp return dummy.next
def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return head dummy = ListNode(0) dummy.next = head while head and head.next: if head.val == head.next.val: head.next = head.next.next else: head = head.next return dummy.next
def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if not head or not head.next: return head dummy = ListNode(0) dummy.next = head pre = dummy while pre.next and pre.next.next: t = pre.next.next pre.next.next = t.next t.next = pre.next pre.next = t pre = pre.next.next return dummy.next
def reverseKGroup(self, head, k): """ :type head: ListNode :type k: int :rtype: ListNode """ if not head or k <= 1: return head dummy = ListNode(0) dummy.next = head pre = dummy count = 0 while head: count += 1 if count % k == 0: pre = self.reverse_list(pre, head.next) head = pre.next else: head = head.next return dummy.next
def sortedListToBST(self, head): """ :type head: ListNode :rtype: TreeNode """ if not head: return head dummy = ListNode(0) dummy.next = head pre = dummy slow = head fast = head while fast and fast.next: fast = fast.next.next slow = slow.next pre = pre.next mid = TreeNode(slow.val) mid.right = self.sortedListToBST(slow.next) pre.next = None mid.left = self.sortedListToBST(dummy.next) return mid
def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ if not head or n < 1: return head dummy = ListNode(0) dummy.next = head pre = dummy length = 1 while length < n: length += 1 head = head.next while head.next: head = head.next pre = pre.next pre.next = pre.next.next return dummy.next
def rotateRight(self, head, k): """ :type head: ListNode :type k: int :rtype: ListNode """ if not head or k < 1: return head k %= self.get_length(head) dummy = ListNode(0) dummy.next = head head = dummy for _ in range(k): if head is None: return null head = head.next pre = dummy while head.next: head = head.next pre = pre.next head.next = dummy.next dummy.next = pre.next pre.next = None return dummy.next
class Solution(object): def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return head dummy = ListNode(0) dummy.next = head head = dummy while head and head.next and head.next.next: if head.next.val == head.next.next.val: val = head.next.val while head.next and head.next.val == val: head.next = head.next.next else: head = head.next return dummy.next s = Solution() l1_1 = ListNode(1) l1_2 = ListNode(1) l1_3 = ListNode(1) l1_4 = ListNode(2) l1_5 = ListNode(3) l1_1.next = l1_2 l1_2.next = l1_3 l1_3.next = l1_4 l1_4.next = l1_5 print s.deleteDuplicates(l1_1)
head = head.next pre = dummy while head.next: head = head.next pre = pre.next head.next = dummy.next dummy.next = pre.next pre.next = None return dummy.next def get_length(self, head): count = 0 while head: head = head.next count += 1 return count s = Solution() l1_1 = ListNode(1) l1_2 = ListNode(2) l1_3 = ListNode(3) l1_4 = ListNode(4) l1_5 = ListNode(5) l1_1.next = l1_2 l1_2.next = l1_3 l1_3.next = l1_4 l1_4.next = l1_5 print s.rotateRight(l1_1, 2) l1_1.next = None print s.rotateRight(l1_1, 1)
if not head: return head first = ListNode(0) first_head = first second = ListNode(0) second_head = second while head: if head.val < x: first_head.next = head first_head = first_head.next else: second_head.next = head second_head = second_head.next head = head.next second_head.next = None first_head.next = second.next return first.next s = Solution() l1_1 = ListNode(1) l1_2 = ListNode(4) l1_3 = ListNode(3) l1_4 = ListNode(2) l1_5 = ListNode(5) l1_6 = ListNode(2) l1_1.next = l1_2 l1_2.next = l1_3 l1_3.next = l1_4 l1_4.next = l1_5 l1_5.next = l1_6 print s.partition(l1_1, 3)
head.next = new_head new_head = head head = tmp return new_head def compare(self, l1, l2): while l1 and l2: if l1.val != l2.val: return False l1 = l1.next l2 = l2.next return True s = Solution() l1_1 = ListNode(1) l1_2 = ListNode(2) l1_3 = ListNode(3) l1_4 = ListNode(4) l1_5 = ListNode(5) l1_1.next = l1_2 l1_2.next = l1_3 l1_3.next = l1_4 l1_4.next = l1_5 print s.isPalindrome(l1_1) l2_1 = ListNode(1) l2_2 = ListNode(2) l2_3 = ListNode(3) l2_4 = ListNode(2) l2_5 = ListNode(1)
l1 = l1.next l2 = l2.next l = l1 or l2 while l: cur = l.val + res res = cur / 10 cur_Node = ListNode(cur % 10) first.next = cur_Node first = first.next l = l.next if res: first.next = ListNode(res) return ans.next s = Solution() l1_1 = ListNode(2) l1_2 = ListNode(4) l1_3 = ListNode(3) l2_1 = ListNode(5) l2_2 = ListNode(6) l2_3 = ListNode(4) l1_1.next = l1_2 l1_2.next = l1_3 l2_1.next = l2_2 l2_2.next = l2_3 s.addTwoNumbers(l1_1, l2_1)