[LeetCode] 25.Reverse Nodes in k-Group

  这题很烦 做完满脑子都是箭头。

  这里head之后 和tail之前 不能用next这种形式来给next复制 会造成循环链表 

tailNode.next = headNode.next  # 4 next point to 2
tailprevNode.next = headNode  # 3 next point to 1(when k is large this node many hard to get)

  

  改了之后还是失败的代码:不是1,6 就是死循环

 

    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        # 0 1
        if head == None or head.next == None:
            return head
        # k == 0
        if k == 0 or k == 1:
            return head
        # swap node
        if k > 1:
            #0 node
            dummy = prevNode = ListNode(0)
            dummy.next = head
            #prepare node
            headNode = prevNode.next
            headnextNode = headNode.next
            tailNode = headNode
            tailprevNode = prevNode
            while tailNode != None:    #break when listnode end
                index = 1
                while index < k:            #break when find tailnode in k rule
                    tailNode = tailNode.next
                    tailprevNode = tailprevNode.next
                    index += 1
                #do swap (comment is k =4)
                prevNode.next = tailNode  # 0 next point to 4
                tailprevNode.next = headNode  # 3 next point to 1(when k is large this node many hard to get)
                headNode.next = tailNode.next  # 1 next point to 5
                tailNode.next = headnextNode  # 4 next point to 2



                #prepare for next
                # head = dummy.next
                prevNode = headNode
                tailprevNode = prevNode
                headNode = headNode.next
                if headNode != None:
                    headnextNode = headNode.next
                else:
                    break
                tailNode = headNode
            return head

 

ac

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

class Solution:
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        # 0 1
        if head == None or head.next == None:
            return head
        # k == 0
        if k == 0 or k == 1:
            return head
        # swap node
        dummy = ListNode(0)
        dummy.next = head
        prevNode = dummy
        currentNode = dummy
        length = 0  #length of head
        while currentNode.next != None :
            length = length +1
            currentNode = currentNode.next
        while(length >= k): #listnode is longer than k
            currentNode = prevNode.next
            for index in range(1,k):
                tempNode = currentNode.next
                currentNode.next = tempNode.next
                tempNode.next = prevNode.next
                prevNode.next = tempNode
            head = dummy.next   #headlist will lose first node
            prevNode = currentNode
            length -= k
        return head

 

posted @ 2019-08-08 16:06  夜歌乘年少  阅读(156)  评论(0编辑  收藏  举报