【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 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个数可以反转,如果没有k个数则返回头结点,否则反转该部分链表并递归求解后来的反转链表部分。为数不多的Hard 题AC

Solution 1

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* reverseKGroup(ListNode* head, int k) {
12         ListNode* cur = head, *tail = nullptr;
13         for(int i = 0; i < k; ++i) {
14             if (!cur)
15                 return head;
16             if (i == k - 1)
17                 tail = cur;
18             cur = cur->next;
19         }
20
21         reverse(head, tail);
22         head->next = reverseKGroup(cur, k);
23
24         return tail;
25     }
26
27     void reverse(ListNode* head, ListNode* tail) {
28         if (head == tail)
29             return;
30         ListNode* pre = head, *cur = pre->next;
31         while(cur != tail) {
32             ListNode* tmp = cur->next;
33             cur->next = pre;
34             pre = cur;
35             cur = tmp;
36         }
37         cur->next = pre;
38     }
39 };

  简化版:

 1 class Solution {
 2 public:
 3     ListNode* reverseKGroup(ListNode* head, int k) {
 4         ListNode *cur = head;
 5         for (int i = 0; i < k; ++i) {
 6             if (!cur) return head;
 7             cur = cur->next;
 8         }
 9         ListNode *new_head = reverse(head, cur);
10         head->next = reverseKGroup(cur, k);
11         return new_head;
12     }
13     ListNode* reverse(ListNode* head, ListNode* tail) {
14         ListNode *pre = tail;
15         while (head != tail) {
16             ListNode *t = head->next;
17             head->next = pre;
18             pre = head;
19             head = t;
20         }
21         return pre;
22     }
23 };

转自:Grandyang

  首先遍历整个链表确定总长度,如果长度大于等于k,那么开始反转这k个数组成的部分链表。交换次数为k-1次,反转后,更新剩下的总长度,迭代进行。

Solution 2

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* reverseKGroup(ListNode* head, int k) {
12         ListNode dummy(-1);
13         dummy.next = head;
14         ListNode* pre = &dummy, *cur = pre;
15         int len = 0;
16         while (cur->next) {
17             ++len;
18             cur = cur->next;
19         }
20         while (len >= k) {
21             cur = pre->next;
22             for (int i = 0; i < k - 1; ++i) {
23                 ListNode* tmp = cur->next;
24                 cur->next = tmp->next;
25                 tmp->next = pre->next;
26                 pre->next = tmp;
27             }
28             pre = cur;
29             len -= k;
30         }
31
32         return dummy.next;
33     }
34 };

原文地址:https://www.cnblogs.com/Atanisi/p/8647712.html

时间: 2024-10-14 05:52:23

【LeetCode】025. Reverse Nodes in k-Group的相关文章

【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】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

【LeetCode】Evaluate Reverse Polish Notation

题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"]

【Leetcode】Evaluate Reverse Polish Notation JAVA

   一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*&

【LeetCode】345. Reverse Vowels of a String 解题小结

题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 应该算不上有难度. class Solution { p

【LeetCode】007 Reverse Interger

题目:LeetCode 007 Reverse Interger 题意:将一个整数的数字反转.保留正负符号. 思路:先将整数变成字符串,然后判断是否为负数,或是否含有’+’,然后从字符串末尾开始累计得到新整数即可. 但是还会有特殊情况,即正向为Int范围内,但反转之后会溢出,因此要进行特判. 代码如下: 1 class Solution { 2 public: 3 int reverse(int x) { 4 int len, flag = 1, i = 0; 5 long long ans =

【LeetCode】402、移除K位数字

1.移除K位数字 题目:402. 移掉K位数字 题目描述: ??给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小.(num 的长度小于 10002 且 ≥ k. num 不会包含任何前导零.) 示例 : 输入: num = "1432219", k = 3 输出: "1219" 解释: 移除掉三个数字 4, 3, 和 2 形成一个新的最小的数字 1219. 输入: num = "10200", k = 1 输

【leetcode】24. Swap Nodes in Pairs

题目描述: Given a linked list, swap every two adjacent nodes and return its head. 解题分析: 解题思路很简单,就是先两两扫描,然后调换顺序即可.这道题的难点在于每个节点的next域指向问题,所以解这样的题最好可以在纸上写一下交换的步骤就不难实现 具体代码: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val

【leetcode】7. Reverse Integer

题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 这道题比较简单,只要注意两个问题:1,输入可能有123,-123两种情况.2,可能会出现值溢出的情况,所以先用long类型处理,决定没有溢出后再转换为int 具体代码: 1 public static int reverse(int x) { 2 String s =""+x; 3