Reverse Nodes in k-Group [LeetCode]
Problem Description: http://oj.leetcode.com/problems/reverse-nodes-in-k-group/
Basic Idea: Do it like reverse a linked list with a counter started by k. Record the tail at the first, then let the tail-> next be the head of rest linked list. This is the recursive version, which is much easier than interative version.
/**
* 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) {
if(k <= )
return head; ListNode *iterate = head;
int num = ;
while(iterate != NULL) {
num ++;
iterate = iterate -> next;
}
if(num < k)
return head; ListNode * new_head = NULL;
ListNode * tail = head;
int count = k;
while(head != NULL && count > ) {
ListNode *pre_head = new_head;
new_head = head;
head = head->next;
count --;
new_head->next = pre_head;
} if(head != NULL)
tail->next = reverseKGroup(head, k); return new_head;
}
};
Reverse Nodes in k-Group [LeetCode]的更多相关文章
- [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 算法思想:基本操作就是链表 ...
- 【Reverse Nodes in k-Group】cpp
题目: 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 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- 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划分 ...
- 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. ...
- Leetcode 25/24 - Reverse Nodes in k-Group
题目描述 Leetcode 24 题主要考察的链表的反转,而 25 题是 24 的拓展版,加上对递归的考察. 对题目做一下概述: 提供一个链表,给定一个正整数 k, 每 k 个节点一组进行翻转,最后返 ...
- [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 ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- LeetCode: Reverse Nodes in k-Group 解题报告
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
随机推荐
- [51NOD1090] 3个数和为0(水题,二分)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1090 找到所有数的和,然后再原数组里二分找符合条件的第三个数. ...
- 安装 SQLManagementStudio_x86_CHS(SQL Server Management Studio) 老提示重启的解决办法
安装 SQL Server Management Studio(SQLManagementStudio_x86_CHS)时,检测时不通过,提示重启电脑,我以为她安装了什么心软件没有重启:所以重启了电脑 ...
- Linux基础※※※※如何使用Git in Linux(一)
参考资料: 1. https://www.linux.com/learn/tutorials/796387-beginning-git-and-github-for-linux-users/ 2. h ...
- js中cookie操作
js中操作Cookie的几种常用方法 * cookie中存在域的概念,使用path和domain区分: * 在同一域中的set和del可以操作同一名称的cookie,但不在同一域中的情况下,则set无 ...
- poj1434Fill the Cisterns!(二分)
链接 题目说给你n个水箱,初始是没有水的,每个的高低位置可能不同,给了你初始的水箱底部所处的水平线位置,问给你V体积水时,水的水平线位置. 直接二分位置p,对于每一个底部低于水平线位置的水箱,里面的水 ...
- 转:printf打印输出2进制
转自:C语言中printf直接打出2进制数是%什么?16进制是什么? void print_2(int val2) { unsigned ; //从低位到高位,低端字节计算机 ; k <= ; ...
- Android控件_使用TextView实现跑马灯效果
一.第一种方式: 通过TextView控件的自身属性实现(但是有缺点就是当多个TextView要实现这种效果的时候,只有第一个才有效果) 实现方法加上下面四个属性: android:singleLi ...
- /dev/sda3: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY
系统强制断电后,出现以下错误: /dev/sda3: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY 启动系统后在字符界面有两个选项,输入root密码进入维护模 ...
- IE和Firefox的Javascript兼容性总结
长久以来JavaScript兼容性一直是Web开发者的一个主要问题.在正式规范.事实标准以及各种实现之间的存在的差异让许多开发者日夜煎熬.为此,主要从以下几方面差异总结IE和Firefox的Javas ...
- fedora 14安装经验
初学linux系统,在win7 系统上安装VMware9.0,并用虚拟机安装fedora.安装了好几次,虽然还是没有彻底通透,但也有一点点心得,特地分享一下: 我安装fedora用于嵌入式ARM开发练 ...