コード例 #1
0
    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
コード例 #2
0
        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
コード例 #3
0
 @Author   :[email protected]
 @File     :237. 删除链表中的节点.py 
 @Description :
"""

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

import leetcode_utils


class Solution(object):
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        # 让这个节点的next指向下个节点
        # 并替换当前这个节点的值
        node.val = node.next.val
        node.next = node.next.next


if __name__ == '__main__':
    list = "[]"
    head1 = leetcode_utils.stringToListNode(list)

    list = Solution.deleteNode(node)
コード例 #4
0
                    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