[leetcode]算法题目 - 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

分析:

链表操作,本身用不上什么高大上的算法或者数据结构。不过其中那些繁杂的小细节很容易搞得头大,很少能有人把这种题目一次写对吧。更可恨的是,就算写完了,隔一天在看自己之前写的思路,依然要费好多脑细胞才能理清楚。。。。。

这道题的思路是,先计算出来链表的长度len,然后len除以k就得到我们总共需要反转几个子链表。反转链表的操作本身不难,难的在于两个子链表衔接的部分。你必须要记录清楚这个子链表的尾巴和头部,并在合适的时机设置它们指向的节点……总之很难用文字描述清楚,直接上代码吧。。就这么几行代码折腾了俩小时才搞定。

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode reverseKGroup(ListNode head, int k) {
14         int len = 0;
15         ListNode headbp = head;
16         while(headbp != null){
17             len++;
18             headbp = headbp.next;
19         }
20         if(len < 2){return head;}
21
22         int reverseTimes = (len / k);
23
24         ListNode myhead = new ListNode(0);
25         myhead.next = head;
26         ListNode curr = myhead;
27         ListNode next = head;
28         ListNode prev = null;
29         ListNode rhead = curr;
30         ListNode rtail = curr.next;
31
32         for(int j=0;j<reverseTimes;j++){
33             rhead = curr;
34             rtail = curr.next;
35             curr = curr.next;
36             next = curr;
37             for (int i = 0; i < k; i++) {
38                 next = next.next;
39                 curr.next = prev;
40                 prev = curr;
41                 curr = next;
42             }
43             rhead.next = prev;
44             rtail.next = next;
45             prev = null;
46             curr = rtail;
47         }
48         return myhead.next;
49     }
50 }
时间: 2024-10-24 22:20:38

[leetcode]算法题目 - Reverse Nodes in k-Group的相关文章

LeetCode解题报告—— Reverse Nodes in k-Group &amp;&amp; Sudoku Solver

1. 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. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple

LeetCode算法题-Reverse Bits(Java实现)

这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:43261596 输出:964176192 说明:43261596以二进制表示为00000010100101000001111010011100, 964176192以二进制表示为00111001011110000010100101000000. 本次解题使用的开发工具是eclipse,jdk使用的版本是1

【leetcode】25. 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, on

LeetCode[Linked List]: 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 nod

【LeetCode】025. 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. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

【LeetCode算法】Reverse Integer

LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note:Assume we are dealing with an environment which could only store int

[leetcode]算法题目 - Sudoku Solver

最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后心里总算有了点底.回溯法的代码一般都是长成下面这样子: void backtracking(int[] arr, int boundary, int current, int[] result) { if(current>=boundary) //到达终止条件 { //判断result是否符合要求

[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 is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

(Java) 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 is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in