zoukankan      html  css  js  c++  java
  • [LeetCode] 25.Reverse Nodes in k-Group-2

      从这位大佬这里抄了解法2:https://www.cnblogs.com/grandyang/p/4441324.html

      

    解法二:

    复制代码
    class Solution {
    public:
        ListNode* reverseKGroup(ListNode* head, int k) {
            ListNode *dummy = new ListNode(-1), *pre = dummy, *cur = pre;
            dummy->next = head;
            int num = 0;
            while (cur = cur->next) ++num;
            while (num >= k) {
                cur = pre->next;
                for (int i = 1; i < k; ++i) {
                    ListNode *t = cur->next;
                    cur->next = t->next;
                    t->next = pre->next;
                    pre->next = t;
                }
                pre = cur;
                num -= k;
            }
            return dummy->next;
        }
    };
    复制代码

    我们也可以使用递归来做,我们用 head 记录每段的开始位置,cur 记录结束位置的下一个节点,然后我们调用 reverse 函数来将这段翻转,然后得到一个 new_head,原来的 head 就变成了末尾,这时候后面接上递归调用下一段得到的新节点,返回 new_head 即可,参见代码如下:

      总结出错的原因,1.没有从listnode的length和k的长度关系来考虑,把问题变得很复杂。2.妄图手动修改listnode的head tail两个节点的四条指针,导致问题复杂化,应该用for循环和tempNode移位。

      抄的代码运行结果:

      

    Success
    Details 
    Runtime: 56 ms, faster than 55.78% of Python3 online submissions for Reverse Nodes in k-Group.
    Memory Usage: 14.6 MB, less than 5.88% of Python3 online submissions for Reverse Nodes in k-Group.
     

    Submission Detail

    81 / 81 test cases passed.
    Status: 

    Accepted

    Runtime: 56 ms
    Memory Usage: 14.6 MB
    Submitted: 0 minutes ago
    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

      40ms:

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def reverseKGroup(self, head, k):
            cur = head
            count = 0
            while cur and count < k:
                cur = cur.next
                count += 1
                
            # cur = 4 
            
            if count == k:
                last = self.reverseKGroup(cur, k)
                while count > 0:
                    next_node = head.next
                    head.next = last
                    last = head
                    head = next_node
                    count -= 1
                head = last
            return head
  • 相关阅读:
    hdu 4460spfa用map来实现
    hdu 2579
    hdu 2845
    hdu 4462
    hdu 4557
    hdu 4639
    URAL 2078 Bowling game
    UVA
    HDU 5773 The All-purpose Zero 脑洞LIS
    Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学
  • 原文地址:https://www.cnblogs.com/alfredsun/p/11322254.html
Copyright © 2011-2022 走看看