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 nodes itself may be changed.

Only constant memory is allowed.

For example,

Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

【分析】

先找到第k个节点,然后递归反转第k节点后面的链表,然后反转head到第k个节点的部分(此时使用非递归方式反转更加方便)。

【题目】

运行时间30ms

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* p = getKthNode(head, k);
        if(head == NULL || p == NULL)
            return head;

        ListNode* res = reverseKGroup(p->next, k);
        p->next = NULL;
        // 从head到res反转
        while(head)
        {
            ListNode* cur = head->next;
            head->next = res;
            res = head;
            head = cur;
        }
        return res;
    }

    ListNode* getKthNode(ListNode* head, int k)
    {
        for(int i = 0; head && i < k - 1; i++)
        {
            head = head->next;
        }
        return head;
    }

};
时间: 2024-10-10 05:37:52

Reverse Nodes in k-Group——解题报告的相关文章

lintcode: k Sum 解题报告

k SumShow Result My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers where sum is target. Calculate how many solutions there are? Ex

Leetcode 151. Reverse Words in a String 解题报告

[Problem] Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. [Idea] 从左往右遍历一次,每当遇到空格暂停

【模拟题(电子科大MaxKU)】解题报告

目录: 1:一道简单题[树形问题](Bzoj 1827 奶牛大集会) 2:一道更简单题[矩阵乘法][快速幂] 3:最简单题[技巧] 话说这些题目的名字也是够了.... 题目: 1.一道简单题 时间1s 题目描述 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1<=N<=100,000) 个农场中的一个,这些农场由N-1条道路连接,并且从任意一个农场都能够到达另外一个农场.道路i连接农场A_i和B_i

LeetCode解题报告—— Reverse Nodes in k-Group &amp;&amp; Sudoku Solver

1. 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

leetCode 25.Reverse Nodes in k-Group (以k个节点为一组反转链表) 解题思路和方法

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 valu

[LintCode] 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. 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

解题报告 之 SOJ2668 C(n,k)

解题报告 之 SOJ2668 C(n,k) Description 求组合数 C ( n , k) 的奇偶性 Input 文件是多case的,每行输入一个 n (1<=n<=10^9)和 k(0<=k<=n) ,当 n 等于 0 且 k 等于 0 时输入结束 Output 对于每一个case,输出一行,为组合数 C ( n , k) 的奇偶性,奇输出1,偶输出0 Sample Input 2 0 2 1 0 0 Sample Output 1 0 Author windy79267

(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

[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

pat解题报告【1074】

1074. Reversing Linked List (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.  For example, given L being 1→2→3→4→5→