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

  这道题个人想法是将链表分段求逆,然后再合并。具体程序如下(有点冗余,有些代码可以合并):

 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* reverseList(ListNode* head, int k)
12     {
13         if(!head || !head->next)
14             return head;
15
16         int len = 0;
17         ListNode *start = head;
18         while(start)
19         {
20             len++;
21             start = start->next;
22             if(len == k)
23                 break;
24         }
25         if(len < k)
26             return head;
27
28         ListNode *postList = start;
29         start = head;
30         ListNode *prev = nullptr;
31         while(start != postList)
32         {
33             ListNode *next = start->next;
34             start->next = prev;
35             prev = start;
36             start = next;
37         }
38
39         start = prev;
40         while(start->next)
41             start = start->next;
42         start->next = postList;
43
44         return prev;
45     }
46
47     ListNode* reverseKGroup(ListNode* head, int k) {
48         ListNode *dummy = new(nothrow) ListNode(INT_MIN);
49         assert(dummy);
50         dummy->next = head;
51         ListNode *preNode = dummy;
52         while(true)
53         {
54             preNode->next = reverseList(head, k);
55             head = preNode->next;
56             int cnt = 0;
57             while(head)
58             {
59                 cnt++;
60                 head = head->next;
61                 preNode = preNode->next;
62                 if(cnt == k)
63                     break;
64             }
65
66             if(cnt < k)
67             {
68                 head = dummy->next;
69                 delete dummy;
70                 dummy = nullptr;
71                 return head;
72             }
73         }
74     }
75 };
时间: 2024-07-31 14:20:38

LeetCode之“链表”:Reverse Nodes in k-Group的相关文章

LeetCode 206 链表 Reverse Linked List

LeetCode 206 链表 Reverse Linked List Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement

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

K组翻转链表 &#183; Reverse Nodes in

[抄题]: 给你一个链表以及一个k,将这个链表从头指针开始每k个翻转一下.链表元素个数不是k的倍数,最后剩余的不用翻转. [思维问题]: [一句话思路]: // reverse head->n1->..->nk->next.. // to head->nk->..->n1->next.. // return n1 每k个转一次,再递归 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [

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】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]算法题目 - 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 -- 删除链表中值为k的元素

本题目比較直接,一次遍历遇到匹配的元素直接删除(通过n.next = n.next.next)就能够了,仅仅是须要考虑到:1.首节点的情况2.末节点的情况 下面为实现: public ListNode RemoveElements(ListNode head, int val) { // null list if(head == null){ return null; } // constains only one node if(head.next == null && head.val

(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

[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