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

2023-09-21 17:57:36 浏览数 (1)

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.边界处理

代码

代码语言:javascript复制
class Solution {
    fun reverseKGroup(head: ListNode?, k: Int): ListNode? {
        if (head?.next == null) {
            return head
         }

        // 找到tail的地址
        var tail = head
        for (i in 0..(k - 1)) {
            // 当这一组链表节点数小于k,直接返回
            if (tail == null) {
                return head
            }
            // 指针逐一后移,一直移到k-1位置=tail
            tail = tail.next
        }

        // 反转该组
        var newHead = reverse(head, tail)

        // head.next = 下一组的 newHead
        head.next = reverseKGroup(tail, k)

        // 返回当前组的newHead
        return newHead
    }


    fun reverse(head: ListNode?, tail: ListNode?): ListNode? {
        var cur = head
        // cur pre
        var pre: ListNode? = null
        // cur next
        var next: ListNode?

        // cur->cur.next
        while (cur != tail) {
            // next持有cur的next节点
            next = cur?.next
            // 反转cur节点的next为pre
            cur?.next = pre
            // 指针向后移到下一个节点
            pre = cur
            cur = next
        }
        // 返回newHead
        return pre
    }

}



class ListNode(var value: Int) {
    var next: ListNode? = null
    override fun toString(): String {
        return "ListNode(value=$value, next=$next)"
    }
}

测试函数:

代码语言:javascript复制
fun main() {
    run {
        val listNode1 = ListNode(1)
        val listNode2 = ListNode(2)
        val listNode3 = ListNode(3)
        val listNode4 = ListNode(4)
        val listNode5 = ListNode(5)
        listNode1.next = listNode2
        listNode2.next = listNode3
        listNode3.next = listNode4
        listNode4.next = listNode5
        listNode5.next = null
        val ans = reverseKGroup(listNode1, 2)
        println(ans)
    }

    run {
        val listNode1 = ListNode(1)
        val listNode2 = ListNode(2)
        val listNode3 = ListNode(3)
        val listNode4 = ListNode(4)
        val listNode5 = ListNode(5)
        listNode1.next = listNode2
        listNode2.next = listNode3
        listNode3.next = listNode4
        listNode4.next = listNode5
        listNode5.next = null
        val ans = reverseKGroup(listNode1, 3)
        println(ans)
    }

}

输出:

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/

源代码:

代码语言:javascript复制
/**
 * Example:
 * var li = ListNode(5)
 * var v = li.`val`
 * Definition for singly-linked list.
 * class ListNode(var `val`: Int) {
 *     var next: ListNode? = null
 * }
 */
class Solution {
    fun reverseList(head: ListNode?): ListNode? {
        var cur = head
        var pre:ListNode? = null
        var next:ListNode? = null
        
        while(cur!=null){
            next = cur.next
            cur.next = pre
            pre = cur
            cur = next
        }
        
        return pre

    }
}

参考资料

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

0 人点赞