LeetCode算法题python解法: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.

题意大概是给定链表和一个参数K,每K个节点反转一次,剩下的节点不够K个则不反转。

这个题目对时间复杂度要求比较高,遍历到列表中通过reverse反转肯定是超时的,只能在链表上直接操作反转。

代码如下:

class Solution:
    def reverseKGroup(self, head, k):
        if head==None:
            return head
        out=[]
        while True:              #遍历链表,将链表放到一个list中,方便后续反转指向的操作
            out.append(head)
            if head.next==None:
                break
            head=head.next
        if k>len(out):
            return out[0]
        st=0
        end=k
        while True:
            for i in range(st+1,end):    #将每K个范围内的节点指向反转
                out[i].next=out[i-1]  

            if end+k<=len(out):          #判断K范围内最后一节点的指向,如果还存在下一个K则指向下个K的最后一个值
                out[st].next=out[end+k-1]
            elif st+k>=len(out):          #如果没有下个K则指向None
                out[st].next=None
            elif st+k<len(out)<end+k:     #如果剩下的数不够凑齐一个K,则指向剩下列表的第一个数
                out[st].next=out[end]
            st+=k
            end+=k
            if len(out) < end:
                break

        if len(out)<2:
            return out[0]
        return out[k-1]

原文地址:https://www.cnblogs.com/slarker/p/9759821.html

时间: 2024-08-07 07:53:47

LeetCode算法题python解法:25. Reverse Nodes in k-Group的相关文章

LeetCode算法题python解法:24. Swap Nodes in Pairs

原题: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as 2->1->4->3. Note: Your algorithm should use only constant extra space. You may not modify the values in

LeetCode算法题python解法:11. Container With Most Water

LeetCode第十一题 Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis fo

LeetCode算法题python解法:23. Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [   1->4->5,   1->3->4,   2->6 ] Output: 1->1->2->3->4->4->5->6 题目大意就是给定多个已经排序的链表合并成一个排序的链表,我这里是将多个链表的值遍

LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)

这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个不同节点的值之间的最小差值.示例: 给定的树[4,2,6,1,3,null,null]由下图表示: 4 / 2 6 / \ 1 3 输出:1 说明:请注意,root是TreeNode对象,而不是数组.该树中的任意节点最小差值为1,它发生在节点1和节点2之间,也发生在节点3和节点2之间. 注意: BS

LeetCode算法题-Reverse Bits(Java实现)

这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:43261596 输出:964176192 说明:43261596以二进制表示为00000010100101000001111010011100, 964176192以二进制表示为00111001011110000010100101000000. 本次解题使用的开发工具是eclipse,jdk使用的版本是1

LeetCode算法题-Valid Perfect Square(Java实现-四种解法)

这是悦乐书的第209次更新,第221篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第77题(顺位题号是367).给定正整数num,写一个函数,如果num是一个完美的正方形,则返回True,否则返回False.例如: 输入:16 输出:true 输入:14 输出:false 注意:不要使用任何内置库函数,例如sqrt. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02 第一种解法 暴力解法

(Java) 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

LeetCode算法题-First Bad Version(Java实现-三种解法)

这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最新版本未通过质量检查.由于每个版本都是基于以前的版本开发的,因此坏版本之后的所有版本也是坏的. 假设您有n个版本[1,2,...,n]并且您想找出第一个坏的版本,这会导致以下所有版本都不好.您将获得一个API bool isBadVersion(版本),它将返回版本是否错误. 实现一个函数来查找第一

LeetCode算法题-Move Zeroes(Java实现-三种解法)

这是悦乐书的第201次更新,第211篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第67题(顺位题号是283).给定一个数组nums,写一个函数将所有0移动到它的末尾,同时保持非零元素的相对顺序.例如: 输入:[0,1,0,3,12] 输出:[1,3,12,0,0] 注意: 您必须在不制作数组副本的情况下就地执行此操作. 最小化操作总数. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02