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

栏目: IT技术 · 发布时间: 6年前

内容简介:给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。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.边界处理

代码

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)"
    }
}


测试函数:

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/

源代码:

/**
 * 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/

Kotlin开发者社区

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

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

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Machine Learning

Machine Learning

Kevin Murphy / The MIT Press / 2012-9-18 / USD 90.00

Today's Web-enabled deluge of electronic data calls for automated methods of data analysis. Machine learning provides these, developing methods that can automatically detect patterns in data and then ......一起来看看 《Machine Learning》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具