LeetCode OJ--Reverse Nodes in k-Group **
http://oj.leetcode.com/problems/reverse-nodes-in-k-group/ 链表 指针
对链表翻转的变形
#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; ListNode *reverse(ListNode * l2)
{
ListNode *n1,*n2,*before;
n1 = l2;
n2 = n1->next;
before = NULL;
while(n2)
{
n1->next = before;
before = n1;
n1 = n2;
n2 = n2->next;
}
n1->next = before;
return n1;
} class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if(k== || head == NULL)
return head; ListNode *ans,*ansTail; bool firstTime = ,flagContinue = ;
ListNode *seghead, *tempNode = head;
while(flagContinue && tempNode)
{
seghead = tempNode = head;
flagContinue = ;
int times = ;
while(tempNode)
{
tempNode = tempNode->next;
times++;
if(times == k- && tempNode)
{
flagContinue = ;
break;
}
}
//break up
if(tempNode)
{
head = tempNode->next;
tempNode->next = NULL;
} if(flagContinue)
{
seghead = reverse(seghead);
}
if(firstTime)
{
ans = ansTail = seghead;
firstTime = false;
}
else
ansTail->next = seghead;
while(ansTail->next)
ansTail = ansTail->next;
}
return ans;
}
}; int main()
{
ListNode *n1 = new ListNode();
ListNode *n2 = new ListNode();
ListNode *n3 = new ListNode();
ListNode *n4 = new ListNode();
ListNode *n5 = new ListNode();
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
ListNode *ans;
Solution myS;
ans = myS.reverseKGroup(NULL,);
return ;
}
LeetCode OJ--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 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
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 蜗牛慢慢爬 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 025 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 an ...
- [LeetCode] 25. 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. k ...
- 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 ...
- [leetcode]25. 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. k ...
随机推荐
- 【数学 技巧】divisor
没考虑重复lcm处理被卡TLE没A真是可惜 题目大意 $n$为$k-可表达的$当且仅当数$n$能被表示成$n$的$k$个因子之和,其中$k$个因子允许相等. 求$[A,B]$之间$k-可表达$的数的个 ...
- Python + Bottle + 谷歌搜索Api 实现简单搜索引擎
1.运行环境 python3 centos7 2.Bottle的使用 使用bottle主要是因为它仅用python自带的库即可实现对web的搭建. bottle源码分析 bottle使用教程 3.代码 ...
- PHPExcel探索之旅
学习地址: https://www.imooc.com/video/8359 下载地址: https://packagist.org/packages/phpoffice/phpexcel 用comp ...
- Java-basic-7-面向对象
继承 在Java中,每个子类只能有一个父类,但可以继承多个接口. 子类继承父类,类定义的时候用extends. 继承接口,用implements. 重写 声明为final的方法不能被重写. 声明为st ...
- atag信息处理
machine_desc->boot_params参数保存的是u-boot传入的启动参数的地址,如果没有传入启动参数,使用如下的默认参数: /* * This holds our default ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(228) Summary Ranges
题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...
- Seven Puzzle Aizu - 0121 (搜索)
7 パズル ime limit1000 ms Memory limit131072 kB OSLinux Source3rd PC Koshien, Final 7 パズルは 8 つの正方形のカードと ...
- hdu-1231 连续最大子序列(动态规划)
Time limit1000 ms Memory limit32768 kB 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj ...
- poj 3050 地图5位数问题 dfs算法
题意:一个5*5地图上面,从任意位置上下左右跳五次,组成一个数.问:不重复的数有多少个? 思路:dfs 从任意位置跳5次,说明每个位置都需要遍历. 组成一个数:number*10+map[dx][dy ...