leetcode 25. Reverse Nodes in k-Group

缺乏、安全感 2022-06-09 08:13 188阅读 0赞

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个元素的一个子链表,然后对每一个子链表做反序处理,对于不足k个元素的最后一组,不做任何处理。

对于本题难点在于反序链表,我这里给出了两种方案,一种是头部插入法实现反序,这个是把元素不断地插入到头部,空间复杂度是O(1),这就是本题的考核要点; 另一种方法就是使用栈,不过这额外会消耗空间,空间复杂度是O(n)。

代码如下:

  1. import java.util.Stack;
  2. /* class ListNode
  3. {
  4. int val;
  5. ListNode next;
  6. ListNode(int x) { val = x; }
  7. }*/
  8. /*
  9. * 这个问题就是链表分割证k个元素的小组,针对每一个小组做反序处理,不足k个的保持原有顺序
  10. *
  11. * 那么问题就简单了,直接分成按照k个元素为一组,然后针对每组做反序处理
  12. *
  13. *
  14. * */
  15. public class Solution
  16. {
  17. public ListNode reverseKGroup(ListNode head, int k)
  18. {
  19. //特殊情况,特殊处理
  20. if(head==null || k<=1)
  21. return head;
  22. ListNode finalHead=new ListNode(-1);
  23. finalHead.next=head;
  24. ListNode iter=finalHead.next,begin=finalHead.next,fa=finalHead;
  25. int count=0;
  26. while(iter!=null)
  27. {
  28. count++;
  29. if(count==k)
  30. {
  31. //切割的k个元素的子链表的end.next设置为null,便于处理
  32. ListNode end=iter;
  33. iter=iter.next;
  34. end.next=null;
  35. ListNode reverHead=reverseListByStack(begin);
  36. //连接链表,fa是上一个子链表的end结点
  37. fa.next=reverHead;
  38. /*因为是反序结点,所以begin反序之后就是end结点
  39. * 这里我们之所以让reverseList返回一个头结点
  40. * 是因为我们反转链表的时候设置了一个头结点
  41. * */
  42. end=begin;
  43. end.next=iter;
  44. //重新设置所有结点
  45. fa=end;
  46. begin=end.next;
  47. count=0;
  48. }else
  49. iter=iter.next;
  50. }
  51. return finalHead.next;
  52. }
  53. /*
  54. * 使用头插法反序链表
  55. * */
  56. public ListNode reverseList(ListNode a)
  57. {
  58. ListNode head=new ListNode(-1);
  59. head.next=a;
  60. //last结点其实是不变化的
  61. //last结点是指的是now的父节点
  62. ListNode last=head.next,now=last.next;
  63. while(now!=null)
  64. {
  65. //last指向下一个代处理的now结点
  66. last.next=now.next;
  67. //插入到head的后面
  68. now.next=head.next;
  69. head.next=now;
  70. //移动now结点
  71. now=last.next;
  72. }
  73. return head.next;
  74. }
  75. /*
  76. * 借助栈来实现反序链表,不过需要非空间
  77. * */
  78. public ListNode reverseListByStack(ListNode a)
  79. {
  80. Stack<ListNode> stack=new Stack<>();
  81. ListNode head=new ListNode(-1);
  82. head.next=a;
  83. ListNode iter=head.next;
  84. while(iter!=null)
  85. {
  86. stack.push(iter);
  87. iter=iter.next;
  88. }
  89. iter=head;
  90. while(stack.isEmpty()==false)
  91. {
  92. iter.next=stack.pop();
  93. iter=iter.next;
  94. }
  95. iter.next=null;
  96. return head.next;
  97. }
  98. }

下面是C++的做法,就是做一个链表分割,然后逆序链表即可。

代码如下:

  1. #include <iostream>
  2. using namespace std;
  3. /*
  4. struct ListNode
  5. {
  6. int val;
  7. ListNode *next;
  8. ListNode(int x) : val(x), next(NULL) {}
  9. };
  10. */
  11. class Solution
  12. {
  13. public:
  14. ListNode* reverseKGroup(ListNode* head, int k)
  15. {
  16. if (head == NULL || head->next == NULL || k<=1)
  17. return head;
  18. ListNode* newHead = new ListNode(-1);
  19. newHead->next = head;
  20. ListNode* i = newHead->next;
  21. ListNode* fa = newHead;
  22. ListNode* begin = newHead->next;
  23. int count = 0;
  24. while (i != NULL)
  25. {
  26. count++;
  27. if (count == k)
  28. {
  29. ListNode* end = i;
  30. i = i->next;
  31. end->next = NULL;
  32. ListNode* tmpHead = reverse(begin);
  33. fa->next = tmpHead;
  34. begin->next = i;
  35. fa = begin;
  36. begin = fa->next;
  37. count = 0;
  38. }
  39. else
  40. i = i->next;
  41. }
  42. return newHead->next;
  43. }
  44. ListNode* reverse(ListNode* head)
  45. {
  46. ListNode* newHead = new ListNode(-1);
  47. newHead->next = head;
  48. ListNode* fa = newHead->next;
  49. ListNode* now = fa->next;
  50. while (now != NULL)
  51. {
  52. fa->next = now->next;
  53. now->next = newHead->next;
  54. newHead->next = now;
  55. now = fa->next;
  56. }
  57. return newHead->next;
  58. }
  59. };

发表评论

表情:
评论列表 (有 0 条评论,188人围观)

还没有评论,来说两句吧...

相关阅读