LeetCode:Reverse Nodes in k-Group
来源:互联网 发布:知乎平均收入 编辑:程序博客网 时间:2024/06/05 19:30
题目链接: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
题目给出一个单链表,以其中每K个结点为一组,逆置每一个组,并且只能使用常量的内存空间。
第一种思路:原地逆置
public class Solution { public ListNode reverseKGroup(ListNode head, int k) { if (head == null || head.next == null) { return head; } ListNode preGroupTail = null, currentGroupHead = head, nextGroupHead = null, current = head; for (int i=0; current != null; i++) { if (( (i+1) % k) == 0) { if (i == k-1) { head = current; } current = current.next; nextGroupHead = current; reverse(preGroupTail, currentGroupHead, nextGroupHead); preGroupTail = currentGroupHead; currentGroupHead = nextGroupHead; continue; } current = current.next; } return head; } // 逆置K个节点 public void reverse(ListNode preGroup, ListNode head, ListNode nextGroup) { ListNode pre = nextGroup; ListNode next = null; while (head != nextGroup) { next = head.next; head.next = pre; pre = head; head = next; } if (preGroup != null) { preGroup.next = pre; } }}
第二种思路:使用数据结构的方法
public class Solution { public ListNode reverseKGroup(ListNode head, int k) { if (head == null || head.next == null) { return head; } LinkedList<ListNode> list = new LinkedList<>(); ListNode current = head, present = null; head = null; while (current != null) { int i = 0; for (; i<k && current!=null; i++) { list.addFirst(current); // 入栈 current = current.next; } if (i < k) { if (head == null) { head = list.removeLast(); present = head; } while (list.size() != 0) { present.next = list.removeLast(); present = present.next; } } else { if (head == null) { head = list.removeFirst(); present = head; present.next = null; } while (list.size() != 0) { present.next = list.removeFirst(); present = present.next; present.next = null; } } } return head; }}
第三种思路:recursive(讨论区的解法,思路比较新奇优雅,只是递归方法占用的内存空间比较大)
public ListNode reverseKGroup(ListNode head, int k) { ListNode curr = head; int count = 0; while (curr != null && count != k) { // find the k+1 node curr = curr.next; count++; } if (count == k) { // if k+1 node is found curr = reverseKGroup(curr, k); // reverse list with k+1 node as head // head - head-pointer to direct part, // curr - head-pointer to reversed part; while (count-- > 0) { // reverse current k-group: ListNode tmp = head.next; // tmp - next head in direct part head.next = curr; // preappending "direct" head to the reversed list curr = head; // move head of reversed part to a new node head = tmp; // move "direct" head to the next node in direct part } head = curr; } return head;}
作业部落
0 0
- LeetCode:Reverse Nodes in k-Group
- LeetCode: Reverse Nodes in k-Group
- LeetCode : Reverse Nodes in k-Group
- leetcode 42: Reverse Nodes in k-Group
- [Leetcode] reverse nodes in K-Group
- [LeetCode]Reverse Nodes in k-Group
- LeetCode-Reverse Nodes in k-Group
- LeetCode - Reverse Nodes in k-Group
- LeetCode | Reverse Nodes in k-Group
- 【leetcode】Reverse Nodes in k-Group
- Leetcode: Reverse Nodes in k-Group
- leetcode Reverse Nodes in k-Group
- [LeetCode] Reverse Nodes in k-Group
- LeetCode题解: Reverse Nodes in k-Group
- 【Leetcode】Reverse Nodes in k-Group
- LeetCode Reverse Nodes in k-Group
- [Leetcode] Reverse Nodes in k-Group (Java)
- LeetCode(25)Reverse Nodes in K-Group
- 优秀的文章&优秀的学习网站之收集手册
- 慌什么,中国的互联网+只是刚刚开始
- (1)Deep learning简介
- 不会裁缝的木工绝对不是好厨师的斜杠中青年的碎碎念
- Intent的应用类型
- LeetCode:Reverse Nodes in k-Group
- android布局
- 如何理解IPD+CMMI+Scrum一体化研发管理解决方案之CMMI篇
- 使用多个段来存储数据(汇编语言)
- 第五周-项目2-1求正差值
- 带缩放动画效果的图片切换的功能实现
- 第五周:C:计算工资。
- DB2 OLAP
- 菜单