【题目】
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_532--K-diff-Pairs-in-an-Array 给定一个整数数组和一个整数 k,您需要找到数组中唯一 k-diff 对的数量。 这里 k-diff 对定义为整数对 (i, j),其中 i 和 j 都是数组中的数字,它们的绝对差为 k。 示例...
LeetCode题解 - Java语言实现-181页.pdf
LeetCode-for-1000K-y practice of LeetCode. DevOps dead. But LC burn in the ash. One question may have several answers. Some answers were copied from Internet or other comments. Be modest. 手写自己的...
leetcode题库所有数据库问题 Leetcode 所有数据库问题:Leetcode 问题 Active-Businesses-LeetCode.png 活跃用户-LeetCode.png 活动-参与者-LeetCode.png 至少合作过三次的演员和导演-LeetCode.png Ads-Performance-...
第四章 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
leetcode 第321题Reversed_Integer Java中的LeetCode逆整数问题解决方案 问题 - 给定一个有符号的 32 位整数 x,返回 x 其数字颠倒。 如果反转 x 导致值超出有符号的 32 位整数范围 [-231, 231 - 1],则返回 0。 ...
reverse-nodes-in-k-group: 解析 pre_for_next 到辅助函数 29:除以两个整数:溢出; 两反 31:下一个排列:再做一次(排序!) 32:最长有效(),使用栈,左推idx 33: search-in-rotated-sorted-array ,比较中间值...
代码:// find K-th Smallest Pair Distancepublic int smallestDistancePair(int[] nums
leetcode添加元素使和等于 LeetCode 数组 Leetcode 0004 寻找两个正序数组的中位数 ----> ----> Leetcode 0027 移除元素 ----> ----> Leetcode 0041 缺失的第一个正数 ----> ----> Leetcode 0048 ...
LeetCode 刷题笔记 with Java 51-100(暗黑版).pdf
java笔试题-LeetCode 刷题笔记 with Java 1-50(暗黑版)
LeetCode-Solutions-in-Swift.zip,swift 5中的leetcode解决方案
leetcode 答案解析 golang解答
解题思路如果最后剩下1-3个石头,你肯定是赢家,如果此时剩下4个石头,你无论拿多少个,对方一定会赢。如果此时剩下5个石头,这时你的最佳策略是拿走一个石头,这是对
leetcode编辑器leetcode-问题 自定义代码演示 leetcode 编辑器配置 代码文件名: $ ! velocityTool . camelCaseName(${question . titleSlug}) 代码模板: ${question . content} package ...t
示例1输出:"1[.]1[.]1[.]1"示例2输出:"255[.]100[.]50[.]0"代码实现本文链接:
leetcode4 中位数leetcode4-中位数-
leetcode 530 LeetCode_530--BST 中的最小绝对差 给定具有非负值的二叉搜索树,找到任意两个节点值之间的最小绝对差。 例子: 笔记: 这个 BST 中至少有两个节点。 这道题和783一样:
示例输入:输出: 1->1->2->3->4->4->5->6解题思路将k个链表建立成一个最小堆,再从堆顶pop()出每一个元素,连接成链表heapq是pyth
leetcode 答案LeetCode-Solutions-and-Answers-in-Python-with-Comments LeetCode 用 Python 编写的答案,每行都带有注释。