当前位置:   article > 正文

LeetCode 25. K 个一组翻转链表(递归,Kotlin)

给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。 k 是一个正整数,

25. K 个一组翻转链表

给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。

k 是一个正整数,它的值小于或等于链表的长度。

如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

示例:

给你这个链表:1->2->3->4->5

当 k = 2 时,应当返回: 2->1->4->3->5

当 k = 3 时,应当返回: 3->2->1->4->5

说明:

你的算法只能使用常数的额外空间。
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。


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.

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.

解题思路

1.分组反转链表操作
2.边界处理

代码

  1. class Solution {
  2. fun reverseKGroup(head: ListNode?, k: Int): ListNode? {
  3. if (head?.next == null) {
  4. return head
  5. }
  6. // 找到tail的地址
  7. var tail = head
  8. for (i in 0..(k - 1)) {
  9. // 当这一组链表节点数小于k,直接返回
  10. if (tail == null) {
  11. return head
  12. }
  13. // 指针逐一后移,一直移到k-1位置=tail
  14. tail = tail.next
  15. }
  16. // 反转该组
  17. var newHead = reverse(head, tail)
  18. // head.next = 下一组的 newHead
  19. head.next = reverseKGroup(tail, k)
  20. // 返回当前组的newHead
  21. return newHead
  22. }
  23. fun reverse(head: ListNode?, tail: ListNode?): ListNode? {
  24. var cur = head
  25. // cur pre
  26. var pre: ListNode? = null
  27. // cur next
  28. var next: ListNode?
  29. // cur->cur.next
  30. while (cur != tail) {
  31. // next持有cur的next节点
  32. next = cur?.next
  33. // 反转cur节点的next为pre
  34. cur?.next = pre
  35. // 指针向后移到下一个节点
  36. pre = cur
  37. cur = next
  38. }
  39. // 返回newHead
  40. return pre
  41. }
  42. }
  43. class ListNode(var value: Int) {
  44. var next: ListNode? = null
  45. override fun toString(): String {
  46. return "ListNode(value=$value, next=$next)"
  47. }
  48. }

测试函数:

  1. fun main() {
  2. run {
  3. val listNode1 = ListNode(1)
  4. val listNode2 = ListNode(2)
  5. val listNode3 = ListNode(3)
  6. val listNode4 = ListNode(4)
  7. val listNode5 = ListNode(5)
  8. listNode1.next = listNode2
  9. listNode2.next = listNode3
  10. listNode3.next = listNode4
  11. listNode4.next = listNode5
  12. listNode5.next = null
  13. val ans = reverseKGroup(listNode1, 2)
  14. println(ans)
  15. }
  16. run {
  17. val listNode1 = ListNode(1)
  18. val listNode2 = ListNode(2)
  19. val listNode3 = ListNode(3)
  20. val listNode4 = ListNode(4)
  21. val listNode5 = ListNode(5)
  22. listNode1.next = listNode2
  23. listNode2.next = listNode3
  24. listNode3.next = listNode4
  25. listNode4.next = listNode5
  26. listNode5.next = null
  27. val ans = reverseKGroup(listNode1, 3)
  28. println(ans)
  29. }
  30. }

输出:

ListNode(value=2, next=ListNode(value=1, next=ListNode(value=4, next=ListNode(value=3, next=ListNode(value=5, next=null)))))
ListNode(value=3, next=ListNode(value=2, next=ListNode(value=1, next=ListNode(value=4, next=ListNode(value=5, next=null)))))

相似的题型

反转链表

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 both?
https://leetcode-cn.com/explore/interview/card/bytedance/244/linked-list-and-tree/1038/

源代码:

  1. /**
  2. * Example:
  3. * var li = ListNode(5)
  4. * var v = li.`val`
  5. * Definition for singly-linked list.
  6. * class ListNode(var `val`: Int) {
  7. * var next: ListNode? = null
  8. * }
  9. */
  10. class Solution {
  11. fun reverseList(head: ListNode?): ListNode? {
  12. var cur = head
  13. var pre:ListNode? = null
  14. var next:ListNode? = null
  15. while(cur!=null){
  16. next = cur.next
  17. cur.next = pre
  18. pre = cur
  19. cur = next
  20. }
  21. return pre
  22. }
  23. }

参考资料

https://leetcode-cn.com/problems/reverse-nodes-in-k-group/
https://leetcode-cn.com/submissions/detail/67275479/


Kotlin开发者社区

专注分享 Java、 Kotlin、Spring/Spring Boot、MySQL、redis、neo4j、NoSQL、Android、JavaScript、React、Node、函数式编程、编程思想、"高可用,高性能,高实时"大型分布式系统架构设计主题。

High availability, high performance, high real-time large-scale distributed system architecture design

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/86960
推荐阅读
相关标签
  

闽ICP备14008679号