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 ...
随机推荐
- 【启发式搜索】Codechef March Cook-Off 2018. Maximum Tree Path
有点像计蒜之道里的 京东的物流路径 题目描述 给定一棵 N 个节点的树,每个节点有一个正整数权值.记节点 i 的权值为 Ai.考虑节点 u 和 v 之间的一条简单路径,记 dist(u, v) 为其长 ...
- COMP9021--6.13
1. break语句和continue语句都可以在循环中使用,且常与选择结构结合使用,以达到在特定条件满足时跳出循环的作用.break语句被执行,可以使整个循环提前结束.而continue语句的作用是 ...
- LeetCode(44) Wildcard Matching
题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single characte ...
- centos新增或删除用户
新增用户snzigod:adduser snzigod 修改snzigod密码:passwd snzigod 删除用户snzigod:userdel snzigod 删除用户以及用户目录: userd ...
- Linux磁盘分区介绍
分区?我们不是已经在BIOS界面分区好了吗?如果领导给你一块磁盘,你怎么用呢?所以就有了分区工具(fdisk和parted),fdisk工具只针对小于2T磁盘分区,且是交互式的:parted很强大,通 ...
- meta-data
<meta-data android:name="string" android:resource="resource specification" ...
- JAVA 基础--开发环境IDEA 搭建
1.下载IDEA (500M+) 2.激活. 在网站http://idea.lanyus.com/中获取注册码,填入Activation code中: 然后点击Activate即可. 3.创建工程前 ...
- JavaScript: 2015 年回顾与展望
链接:http://www.sitepoint.com/javascript-2015-review/ JavaScript经历了一个不平凡的一年.尽管到5月份已经20年了,关于JS的新闻.项目和兴趣 ...
- 十分钟了解HTTPS协议
概念 HTTP协议上添加一层SSL/TLS协议进行加密,保证用户与web站点之间的数据传输时密文,而不是明文. PS:HTTPS协议 = HTTP协议 + SSL(Secure Sockets Lay ...
- 聊聊、Jstack 解决生产问题
最近项目很多,所在公司是一家金融企业.从 APP 端到 基金公司,整个体系涉及到很多系统.而我所负责的,正好是整个体系尾部,业务核心.前段时间,隔几天总会有用户购买理财产品失败,但是日志里面没有任何异 ...