【题目】
Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.
If the number of nodes is not a multiple ofkthen 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
Fork= 2, you should return:2->1->4->3->5
Fork= 3, you should return:3->2->1->4->5
【题意】
给定一个链表,一次反转链表前k个节点,并返回它的修改链表。
如果节点的数量是不k的倍数则最终留出节点应该保持原样,每K个一反转,不到k个不用反转。
【分析】
无
【代码】
/*********************************
* 日期:2014-01-31
* 作者:SJF0115
* 题号: Reverse Nodes in k-Group
* 来源:http://oj.leetcode.com/problems/reverse-nodes-in-k-group/
* 结果:AC
* 来源:LeetCode
* 总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if (head == NULL || k < 2){
return head;
}
ListNode *dummy = (ListNode*)malloc(sizeof(ListNode));
dummy->next = head;
ListNode *pre = dummy,*cur = NULL,*tail = NULL;
//统计节点个数
int count = 0;
while(pre->next != NULL){
pre = pre->next;
count++;
}
//反转次数
int rCount = count / k;
int index = 0;
//反转元素的前一个
pre = dummy;
//反转元素第一个即翻转后的尾元素
tail = dummy->next;
//共进行rCount次反转
while(index < rCount){
int i = k - 1;
//反转
while(i > 0){
//删除cur元素
cur = tail->next;
tail->next = cur->next;
//插入cur元素
cur->next = pre->next;
pre->next = cur;
i--;
}
pre = tail;
tail = pre->next;
index++;
}
return dummy->next;
}
};
int main() {
Solution solution;
int A[] = {1,2,3,4,5};
ListNode *head = (ListNode*)malloc(sizeof(ListNode));
head->next = NULL;
ListNode *node;
ListNode *pre = head;
for(int i = 0;i < 5;i++){
node = (ListNode*)malloc(sizeof(ListNode));
node->val = A[i];
node->next = NULL;
pre->next = node;
pre = node;
}
head = solution.reverseKGroup(head->next,5);
while(head != NULL){
printf("%d ",head->val);
head = head->next;
}
return 0;
}
【代码】
分享到:
相关推荐
第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer ...25. Reverse Nodes in k-Group 26. Remove Dupli
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现...
leetcode题库所有数据库问题 Leetcode 所有数据库问题:Leetcode 问题 Active-Businesses-LeetCode.png 活跃用户-LeetCode.png 活动-参与者-LeetCode.png 至少合作过三次的演员和导演-LeetCode.png Ads-Performance-...
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest ...Reverse ...25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
leetcode 不会394.-解码字符串-c-Leetcode 给定一个编码字符串,返回它的解码字符串。 编码规则是:k[encoded_string],其中方括号内的encoded_string 正好重复k 次。 请注意,k 保证为正整数。 您可以假设输入字符...
leetcode有效LeetCode-1108.-Defanging-an-IP-Address 给定一个有效的 (IPv4) IP 地址,返回该 IP 地址的去角版本。 去污点的 IP 地址会替换每个句点“.”。 和 ”[。]”。 示例 1: 输入:address = "1.1.1.1" 输出...
leetcode 530 530.-BST-Leetcode 中的最小绝对差 给定二叉搜索树 (BST) 的根,返回树中任意两个不同节点的值之间的最小绝对差。 示例 1: 输入:root = [4,2,6,1,3] 输出:1 示例 2: 输入:root = [1,0,48,null,...
第 338 章338.-Counting-Bits-LeetCode 计数位-LeetCode
vs code LeetCode 插件
Leetcode 题解.pdf
这是来自LeetCode的已解决任务的存储库使用Java语言解决任务 CoinChange.java - //leetcode.com/problems/coin-change/ ProductOfArrayExceptSelf.java - //leetcode....
My Solutions to Leetcode Database problems. 我的 Leetcode 数据.zip
LeetCode 刷题
84. 柱状图中最大的矩形 - 力扣(LeetCode).html
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
Leetcode101.zip
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。