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

==================

思路:

  没有什么坑,就是编码麻烦,

代码:

void help_reverseKGroup(ListNode *prev,ListNode *curr,int k){
ListNode *p = prev->next;
prev->next = curr->next;
for(int i = ;i<k;i++){
ListNode *tmp = p->next;
p->next = prev->next;
prev->next = p;
p = tmp;
}
}
ListNode* reverseKGroup(ListNode* head, int k) {
if(k<=) return head;
if(head==nullptr || head->next==nullptr)return head;
ListNode dummy(-);
dummy.next = head;
ListNode *curr,*prev;
curr = head;
prev = &dummy; while(curr){
int i = ;
for(;i<k;i++){
if(curr->next) curr = curr->next;
else break;
}
if(i!=k) break;
//showList(dummy.next);cout<<"k1"<<endl; help_reverseKGroup(prev,curr,k); //showList(dummy.next);cout<<"k2"<<endl;
for(i = ;i<k;i++){
prev = curr;
curr = curr->next;
}
//showList(head);cout<<"k3"<<endl;
}///while
return dummy.next;
}

25. Reverse Nodes in k-Group的更多相关文章

  1. [Leetcode] 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 ...

  2. Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转

    问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...

  3. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  4. [Leetcode][Python]25: Reverse Nodes in k-Group

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...

  5. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  6. LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)

    题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description   Problem :将一个有序list划分 ...

  7. 25.Reverse Nodes in k-Group (List)

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  8. 25. Reverse Nodes in k-Group (JAVA)

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k  ...

  9. 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  10. 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

随机推荐

  1. Log4J 使用实战

    前言: 日志在开发和服务中扮演重要的角色, 有人用来追查/分析问题, 有人通过日志, 来记录重要的信息. 日志是数据分析和统计最重要的数据来源. 在Java领域, Log4j日志框架成为java开发人 ...

  2. 矩阵基本运算的 Python 实现

    from...import与import区别在于import直接导入指定的库,而from....import则是从指定的库中导入指定的模块 import...as则是将import A as B,给予 ...

  3. 2015GitWebRTC编译实录7

    2015.07.20 libvoiceengine 编译通过去除了mock测试代码,mock是用来进行测试的,意义不大.另外会报一个常量错误,需要定义WEBRTC_MAC宏,只定义WEBRTC_IOS ...

  4. AndroidStudio导入Android-PullToRefresh

    方法已经写到我的微信公众号中,公众号二维码在下面 本人联系方式: 更多精彩分享,可关注我的微信公众号: 若想给予我分享更多知识的动力,请扫描下面的微信打赏二维码,谢谢!: 微信号:WeixinJung ...

  5. ubuntu12.04 修复Grub2

    电脑双系统,但是把win7重装了之后,会发现grub坏了,只能进入win7. 遇到过好几次,虽然每次都成功解决问题了,但是都花费了不少时间. 所以,总结一下,基本是从网上找到的方法,有的行不通,有的可 ...

  6. leetcode 121. Best Time to Buy and Sell Stock ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. 黑马程序员——JAVA基础之程序控制流结构之循环结构,循环嵌套

    ------- android培训.java培训.期待与您交流! ---------- 循环结构: 代表语句:while ,do while ,for while语句格式 : while(条件表达式) ...

  8. 将HTML段赋值给PHP变量的便捷方法,不使用转义字符

    <?php $b='12'; $a=<<<sss <html> <head> </head> <body> <i>& ...

  9. PostgreSQL连接Python

    安装 PostgreSQL可以用Python psycopg2模块集成. sycopg2是Python编程语言的PostgreSQL数据库的适配器. 其程序代码少,速度快,稳定.不需要单独安装这个模块 ...

  10. Knockout JS 入门示例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...