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
==================
思路:
没有什么坑,就是编码麻烦,
代码:
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的更多相关文章
- [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 ...
- 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 算法思想:基本操作就是链表 ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- 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 ...
- 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划分 ...
- 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 ...
- 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 ...
- 蜗牛慢慢爬 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. ...
- 【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 ...
随机推荐
- PR 创建
REPORT Z_CREATE_PR. DATA: BEGIN OF GT_DATA1 OCCURS 0, BSART TYPE STRING, "凭证类型 ...
- linux 中 chmod/chown/cngrp的用法与区别
1.chgrp(转变文件所属用户组) chgrp 用户组 文件名 .若是整个目次下的都改,则加-R参数用于递归. 如:chgrp -R user smb.conf 2.chown(转变文件拥有者) ...
- activiti 任务节点 处理人设置【转】
转自http://blog.csdn.net/qq_30739519/article/details/51225067 1.1.1. 前言 分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自 ...
- UI组件(思维导图)
- android XML解析器全解案例
1.使用pull解析 package com.example.myxml; import java.io.InputStream; import java.util.ArrayList; import ...
- InnoDB Double write
记得刚开始看InnoDB文档的时候,Double Write一节(其实只有一小段)就让我很困惑.无奈当时内力太浅,纠缠了很久也没弄明白.时隔几个月,重新来整理一下. 涉及到的概念:Buffer Poo ...
- sql compact 使用EF无法更新的问题?
1.问题一是表中没有主键会报一些莫名其妙的错误. 2.数据库文件被默认复制到了Debug/Release目录,实际调试或运行时发现原有数据库没有被更新.
- httplib
可爆破目录 import httplib import urllib def sendhttp(): data = urllib.urlencode({'@number': 12524, '@type ...
- CorelDRAW 实现蒙版效果的方法
CorelDRAW能够实现很多意想不到的小效果,其中包括了位图图像软件的处理功能,蒙版效果就是其中的一项.作为矢量图形处理软件,从理论上讲它并不具备蒙板技术,然而只是我们平常没有用到而已,利用图框精确 ...
- 输入n行整数,每行的个数不确定,整数之间用逗号分隔
/*===================================== 输入n行整数,每行的个数不确定. 每行内部两个数之间用逗号隔开. 例如输入数据如下: 6 1,3,5,23,6,8,14 ...