新博文地址:
[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 return its modified list.
If the number of nodes is not a multiple of k then 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
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
If the number of nodes is not a multiple of k then 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
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
跟这道题很像,只不过加了一个迭代的要求,很直接的想法:
1.算出需要转置区间的个数
2.对每一次转置区间找到其前驱和后驱
3.对每个转置区间进行转置
其实,找前驱和后驱的代码是可以优化的,留在第二遍再优化,而且这样写虽然代码不够简洁,但是可读性稍稍好一些。
public ListNode reverseKGroup(ListNode head, int k) { int length = getLength(head); if(length < k || k <= 1){ return head; } ListNode hhead = new ListNode(0); hhead.next = head; for(int i = 0; i < length / k; i++){ ListNode pre = getStartInLoopN(hhead, i, k); ListNode post = getEndInLoopN(pre, k); ListNode iPre = pre; ListNode iNode = pre.next; ListNode iPost = iNode.next; while(iNode != post){ if(iNode == pre.next){ iNode.next = post; iPre = iNode; iNode = iPost; iPost = iNode.next; }else if(iNode.next == post){ pre.next = iNode; iNode.next = iPre; iNode = post; }else{ iNode.next = iPre; iPre = iNode; iNode = iPost; iPost = iNode.next; } } } return hhead.next; } private ListNode getStartInLoopN(ListNode hhead,int n,int k){ for(int i = 0 ; i < n*k; i++){ hhead = hhead.next; } return hhead; } private ListNode getEndInLoopN(ListNode startNode,int k){ for(int i = 0 ; i < k + 1; i++){ startNode = startNode.next; } return startNode; } private int getLength(ListNode head){ int count = 0; while(head != null){ head = head.next; count++; } return count ; }
相关推荐
js js_leetcode题解之25-reverse-nodes-in-k-group.js
c c语言_leetcode 0025_reverse_nodes_in_k_group.zip
第四章 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
reverse-nodes-in-k-group: 解析 pre_for_next 到辅助函数 29:除以两个整数:溢出; 两反 31:下一个排列:再做一次(排序!) 32:最长有效(),使用栈,左推idx 33: search-in-rotated-sorted-array ,比较中间值...
lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest ...Reverse ...Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
多线程 leetcode 前言 每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium Remove Nth ...Nodes in ...k ...Reverse Nodes in k-Group Trapping Rain Water
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/...
leetcode添加元素使和等于 总结 按照类别分类来刷 刷当前题的时候,看下『题目描述』最底下有个『相似题目』,这些题的思路大致也相当 ...reverse-nodes-in-k-group 二叉树 实现一个二叉树 二叉树二叉树的
- Swap Nodes in Pairs / Reverse Nodes in k-Group: 这两个问题涉及到在链表中按特定规则交换节点或反转节点组。 - Remove Duplicates from Sorted Array / Remove Element: 删除排序数组中的重复项,或从数组中...
- **2.2.9 Reverse Nodes in k-Group** - 每k个一组反转链表。 - 实现思路:维护一个指针指向当前组的前一个节点,依次反转每一组。 - **2.2.10 Copy List with Random Pointer** - 复制一个带随机指针的链表。...
5. K 个一组翻转链表(Reverse Nodes in K-Group, 如LeetCode的第25题):将链表中的节点每K个一组进行反转。 解决这些题目时,通常需要掌握以下技巧: - 使用快慢指针(Floyd's Tortoise and Hare)来检测链表环。...
问题 ... 025_Reverse_Nodes_in_k-Group 026_Remove_Duplicates_from_Sorted_Array 027_Remove_Element 028_Implement_strStr() 029_Divide_Two_Integers 030_Substring_with_Concatenation_of
20. SwapNodesinPairs: 在链表中交换每两个相邻节点,这需要处理好链表的指针。 21. ReverseNodesink-Group: 以k个节点为一组翻转链表,这是一个在链表上的递归或迭代问题。 22. RemoveElement: 从数组中移除特定...