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

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的倍数则最终留出节点应该保持原样,每K个一反转,不到k个不用反转。

【分析】

【代码】

/*********************************
*   日期:2014-01-31
*   作者:SJF0115
*   题号: Reverse Nodes in k-Group
*   来源:http://oj.leetcode.com/problems/reverse-nodes-in-k-group/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *reverseKGroup(ListNode *head, int k) {
        if (head == NULL || k < 2){
            return head;
        }

        ListNode *dummy = (ListNode*)malloc(sizeof(ListNode));
        dummy->next = head;

        ListNode *pre = dummy,*cur = NULL,*tail = NULL;
        //统计节点个数
        int count = 0;
        while(pre->next != NULL){
            pre = pre->next;
            count++;
        }
        //反转次数
        int rCount = count / k;
        int index = 0;
        //反转元素的前一个
        pre = dummy;
        //反转元素第一个即翻转后的尾元素
        tail = dummy->next;
        //共进行rCount次反转
        while(index < rCount){
            int i = k - 1;
            //反转
            while(i > 0){
                //删除cur元素
                cur = tail->next;
                tail->next = cur->next;
                //插入cur元素
                cur->next = pre->next;
                pre->next = cur;
                i--;
            }
            pre = tail;
            tail = pre->next;
            index++;
        }
        return dummy->next;
    }
};
int main() {
    Solution solution;
    int A[] = {1,2,3,4,5};
    ListNode *head = (ListNode*)malloc(sizeof(ListNode));
    head->next = NULL;
    ListNode *node;
    ListNode *pre = head;
    for(int i = 0;i < 5;i++){
        node = (ListNode*)malloc(sizeof(ListNode));
        node->val = A[i];
        node->next = NULL;
        pre->next = node;
        pre = node;
    }
    head = solution.reverseKGroup(head->next,5);
    while(head != NULL){
        printf("%d ",head->val);
        head = head->next;
    }
    return 0;
}

【代码】

时间: 2024-09-26 22:47:32

[LeetCode]25.Reverse Nodes in k-Group的相关文章

LeetCode 25 Reverse Nodes in k-Group(在K组链表中反转结点)

原文 给定一个链表,在一定时间内反转这个链表的结点,并返回修改后的链表. 如果结点数不是K的倍数,那么剩余的结点就保持原样. 你不应该在结点上修改它的值,只有结点自身可以修改. 只允许使用常量空间. 例如 给定链表: 1->2->3->4->5 对于 k = 2,你应该返回: 2->1->4->3->5 对于 k = 3,你应该返回: 3->2->1->4->5 翻译 Given a linked list, reverse the

[LeetCode]24.Swap Nodes in Pairs

[题目] Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the lis

[LeetCode] Subarray Product Less Than K 子数组乘积小于K

Your are given an array of positive integers nums. Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k. Example 1: Input: nums = [10, 5, 2, 6], k = 100 Output: 8 Explanation: The 8

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 7 Reverse Integer(翻转整数)

翻译 翻转一个整型数 例1:x = 123, 返回 321 例2:x = -123, 返回 -321 原文 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? (来自LeetCode官网) Here are some good questions to ask before coding. Bonus poi

[LeetCode]151.Reverse Words in a String

题目 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. click to show clarification.

[LeetCode]92.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

[LeetCode]--345. Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". Note: The vowels does not incl

leetcode 7 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throu