【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

将k个元素为一个分组进行翻转

确定元素是否足够k个,如果足够,进行翻转(每次把当前元素移动到开头位置)

如果不够保持原样

 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
13         if(k<=1) return head;
14
15         ListNode *front=new ListNode(0);
16         front->next=head;
17
18         ListNode *cur,*next,*result;
19         cur=head;
20         result=head;
21
22
23         while(cur!=NULL)
24         {
25             int count=1;
26             ListNode *tmp=cur;
27
28             //当前元素以及后续元素之和是否够k个
29             while(cur!=NULL)
30             {
31                 cur=cur->next;
32                 if(cur==NULL) break;
33
34                 count++;
35                 if(count==k)
36                 {
37                     break;
38                 }
39             }
40
41
42
43             if(count==k)
44             {
45                 //此时cur指向需要翻转的最后一个元素,last表示下次开始时的元素
46                 ListNode *last=cur->next;
47                 cur=tmp;
48
49                 //表示是否需要更新头结点,作为返回值
50                 bool isfirst=front->next==head;
51
52                 //每次把当前的元素移动到开头
53                 while(cur->next!=last)
54                 {
55                     //获得下一个元素
56                     next=cur->next;
57                     //当前元素指向下下个元素
58                     cur->next=next->next;
59                     //下一个元素移动到开头
60                     next->next=front->next;
61                     //指向开头元素
62                     front->next=next;
63
64                     if(isfirst) result=front->next;
65                 }
66             }
67             else
68             {
69                 break;
70             }
71
72             front=cur;
73             cur=last;
74         }
75
76         return result;
77     }
78 };
时间: 2024-12-17 19:47:19

【leetcode】Reverse Nodes in k-Group的相关文章

【leetcode】reverse Nodes in k-groups

问题: 给定一个链表的头指针,以及一个整数k,要求将链表按每k个为一组,组内进行链表逆置.少于k个的部分不做处理. 分析: 个人觉得问题的重点是熟悉链表的就地逆置操作,就是头插法.其他的考察点如果还有的话,就的细心程度. 实现: void reverseList(ListNode *&pre, ListNode *head) { ListNode *tail = NULL; while (head) { ListNode* next = head->next; head->next =

【leetcode】Reverse Nodes in k-Group (hard)☆

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】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

【LeetCode】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

【LeetCode】Reverse Linked List II 解题报告

[题目] Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

【LeetCode】Swap Nodes in Pairs

题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list,

【LeetCode】Swap Nodes in Pairs (3 solutions)

Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the val