Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of

class Solution {
public:
    ListNode *reverseKGroup(ListNode *head, int k) {
        if (!head || !(head->next) || k < 2)
            return head;  

        // count k nodes
        ListNode *nextgp = head;
        for (int i = 0; i < k; i++)
            if (nextgp)
                nextgp = nextgp->next;
            else
                return head;  

        // reverse
        ListNode *prev = NULL, *cur = head, *next = NULL;
        while (cur != nextgp) {
            next = cur->next;
            if (prev)
                cur->next = prev;
            else
                cur->next = reverseKGroup(nextgp, k);
            prev = cur;
            cur = next;
        }
        return prev;
    }
};

  

时间: 2024-11-10 00:00:41

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of的相关文章

LeetCode[Linked List]: Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

LeetCode[Linked List]: Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

【LeetCode】27.Linked List—Reverse Linked List l链表逆置

Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? 思路: 重塑链表,将首结点逐个后移,移动过程中遇到的结点都

Reversed Linked List(Reverse a singly linked list)

struct ListNode { int m_nKey; ListNode* next; } ListNode* reverseList(ListNode* pHead) { ListNode* pReversedHead = nullptr; ListNode* pNode = pHead; ListNode* pPrev = nullptr; while(pNode != nullptr){ ListNode* pNext = pNode->next; if(pNext == nullpt

863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

[抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Return a list of the values of all nodes that have a distance K from the target node.  The answer can be returned in any order. Example 1: Input: root = [3

Reverse Nodes in k-Group

[LeetCode] 题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then

[LeetCode] 25. Reverse Nodes in k-Group ☆☆☆

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

【LeetCode】025. Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in