c/c++语言开发共享C++实现LeetCode(25.每k个一组翻转链表)

[leetcode] 25. reverse nodes in k-group 每k个一组翻转链表given a linked list, reverse the nodes of a linked


[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 the end should remain as it is.

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

note:

  • only constant extra memory is allowed.
  • you may not alter the values in the list’s nodes, only nodes itself may be changed.

这道题让我们以每k个为一组来翻转链表,实际上是把原链表分成若干小段,然后分别对其进行翻转,那么肯定总共需要两个函数,一个是用来分段的,一个是用来翻转的,以题目中给的例子来看,对于给定链表 1->2->3->4->5,一般在处理链表问题时,大多时候都会在开头再加一个 dummy node,因为翻转链表时头结点可能会变化,为了记录当前最新的头结点的位置而引入的 dummy node,加入 dummy node 后的链表变为 -1->1->2->3->4->5,如果k为3的话,目标是将 1,2,3 翻转一下,那么需要一些指针,pre 和 next 分别指向要翻转的链表的前后的位置,然后翻转后 pre 的位置更新到如下新的位置:

1->1->2->3->4->5
|        |  |
pre      cur next

1->3->2->1->4->5
|     |  |
cur   pre next

以此类推,只要 cur 走过k个节点,那么 next 就是 cur->next,就可以调用翻转函数来进行局部翻转了,注意翻转之后新的 cur 和 pre 的位置都不同了,那么翻转之后,cur 应该更新为 pre->next,而如果不需要翻转的话,cur 更新为 cur->next,代码如下所示:

解法一:

  class solution {  public:      listnode* reversekgroup(listnode* head, int k) {          if (!head || k == 1) return head;          listnode *dummy = new listnode(-1), *pre = dummy, *cur = head;          dummy->next = head;          for (int i = 1; cur; ++i) {              if (i % k == 0) {                  pre = reverseonegroup(pre, cur->next);                  cur = pre->next;              } else {                  cur = cur->next;              }          }          return dummy->next;      }      listnode* reverseonegroup(listnode* pre, listnode* next) {          listnode *last = pre->next, *cur = last->next;          while(cur != next) {              last->next = cur->next;              cur->next = pre->next;              pre->next = cur;              cur = last->next;          }          return last;      }  };

我们也可以在一个函数中完成,首先遍历整个链表,统计出链表的长度,然后如果长度大于等于k,交换节点,当 k=2 时,每段只需要交换一次,当 k=3 时,每段需要交换2此,所以i从1开始循环,注意交换一段后更新 pre 指针,然后 num 自减k,直到 num<k 时循环结束,参见代码如下:

解法二:

  class solution {  public:      listnode* reversekgroup(listnode* head, int k) {          listnode *dummy = new listnode(-1), *pre = dummy, *cur = pre;          dummy->next = head;          int num = 0;          while (cur = cur->next) ++num;          while (num >= k) {              cur = pre->next;              for (int i = 1; i < k; ++i) {                  listnode *t = cur->next;                  cur->next = t->next;                  t->next = pre->next;                  pre->next = t;              }              pre = cur;              num -= k;          }          return dummy->next;      }  };

我们也可以使用递归来做,用 head 记录每段的开始位置,cur 记录结束位置的下一个节点,然后调用 reverse 函数来将这段翻转,然后得到一个 new_head,原来的 head 就变成了末尾,这时候后面接上递归调用下一段得到的新节点,返回 new_head 即可,参见代码如下:

解法三:

  class solution {  public:      listnode* reversekgroup(listnode* head, int k) {          listnode *cur = head;          for (int i = 0; i < k; ++i) {              if (!cur) return head;              cur = cur->next;          }          listnode *new_head = reverse(head, cur);          head->next = reversekgroup(cur, k);          return new_head;      }      listnode* reverse(listnode* head, listnode* tail) {          listnode *pre = tail;          while (head != tail) {              listnode *t = head->next;              head->next = pre;              pre = head;              head = t;          }          return pre;      }  };

到此这篇关于c++实现leetcode(25.每k个一组翻转链表)的文章就介绍到这了,更多相关c++实现每k个一组翻转链表内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享C++实现LeetCode(25.每k个一组翻转链表),都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/648761.html

(0)
上一篇 2021年7月13日
下一篇 2021年7月13日

精彩推荐