leetcode第24题--Reverse Nodes in k-Group

problem:

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是2.这里的k是任意的整数。

子函数先将一个链表反转,然后在主函数里调用。链表的反转基本上四个语句就搞定了,自己想死也想不出来啊。不过算是学习了。以后碰到类似的应该要会利用。参考了一位java的代码。这里我改成了C++的代码。java的主页在http://www.cnblogs.com/lichen782/p/leetcode_Reverse_Nodes_in_kGroup.html。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
private:
ListNode *reverseList(ListNode *pre, ListNode *tail) // tail 为第 k+1
{
    ListNode *last = pre -> next;
    ListNode *cur = last -> next;
    while(cur != tail)
    {
        last->next = cur -> next;
        cur -> next = pre -> next;
        pre -> next = cur;
        cur = last -> next;
    }
    return last; // 这个last为第k,也就是每组的最后一个,作为下一组的头
}
public:
ListNode *reverseKGroup(ListNode *head, int k)
{
    int i = 0;
    ListNode *dummy = new ListNode(0);
    ListNode *pre = dummy;
    dummy -> next = head;
    while(head)
    {
        i++;
        if(i % k == 0)
        {
            pre = reverseList(pre, head -> next); // 此时head指向第k个,就是每组的最后一个,所以尾应该是head的next
            head = pre -> next; // head应该是前一组的最后一个的下一个
        }
        else
            head = head -> next;
    }
    return dummy -> next;
}
};

本例主要学习如何反转一个list

时间: 2024-12-16 08:29:00

leetcode第24题--Reverse Nodes in k-Group的相关文章

leetcode_25题——Reverse Nodes in k-Group (链表)

Reverse Nodes in k-Group Total Accepted: 34390 Total Submissions: 134865My Submissions Question Solution 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 t

LeetCode之“链表”: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 node

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

leetcode第七题--Reverse Integer

Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 终于什么都没参考就一次Accept了.可能是这题比较简单,同时自己也进步了一点点,leetcode就是这样给我们增加信心的吧. 我是这样想的,利用除10和模10两个操作,将每个数字分离在vec中,然后相应的乘以10的倍数输出就行.如果这个数在-10与10之间直接返回就好. 代码如下: class S

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 list,

leetcode第23题--Swap Nodes in Pairs

Problem: 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

网络流24题之最长k可重线段集问题

对于每个线段拆成两个点,如同之前一样建图,由于可能出现垂直于x轴的 所以建图由i指向i~ 继续最小费用最大流 By:大奕哥 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=10000005,inf=1e9; 4 int head[N],d[N],f[N],l1[N],r1[N],l2[N],r2[N],a[N],s=1e9,t,n,k,cnt=-1; 5 long long cost; 6 bool v[N]; 7

*LOJ#6227. 「网络流 24 题」最长k可重线段集问题

$n \leq 500$条平面上的线段,问一种挑选方法,使得不存在直线$x=p$与挑选的直线有超过$k$个交点,且选得的直线总长度最长. 横坐标每个点开一个点,一条线段就把对应横坐标连一条容量一费用(-长度)的边:点$x$向点$x+1$连一条容量$k$费用0的边.这里的$k$边限制的是直线上其他不经过这里的地方. 这里有个trick就是有与$x$轴垂直的线段.直接判掉会wa.为此把坐标扩大两倍,如果$l=r$那么$r++$否则$l++$,相当于把一个点拆成两个. 原文地址:https://www

「网络流 24 题」最长 k 可重区间集

给定区间集合$I$和正整数$k$, 计算$I$的最长$k$可重区间集的长度. 区间离散化到$[1,2n]$, $S$与$1$连边$(k,0)$, $i$与$i+1$连边$(k,0)$, $2n$与$T$连边$(k,0)$. 对于每个区间$(l,r)$, $l$与$r$连边$(1,l-r)$. 最小费用相反数就为最大长度 #include <iostream> #include <sstream> #include <algorithm> #include <cst