【LeetCode】每日一题:K个一组反转链表

解题思路

其实更像一个模拟题,但是有两个地方的边界一直没有处理好导致卡了很久。

AC代码

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        def rotate(prev, tail):
        	# 少了一个end最开始,tail这个节点在循环修改next时候会变,从而导致循环终止条件错误,应该用一个定量记下来
            end = tail.next
            last = tail.next
            curr = prev.next
            res = prev.next
            while curr != end:
                temp = curr.next
                curr.next = last
                last = curr
                curr = temp
            prev.next = tail
            return res

        dummyhead = ListNode(0)
        dummyhead.next = head

        tail = dummyhead
        indexhead = dummyhead
        count = 0
    
        while indexhead:
            for _ in range(k):
            # 这个地方最开始写的是tail,其实应该是判断后面有没有了,后面没有就不能继续计数了
                if tail.next:
                    tail = tail.next
                    count += 1
                else:
                    break
            if count == k:
                tail = indexhead = rotate(indexhead, tail)
                count = 0
            else:
                break


        

        return dummyhead.next

官方题解

官方题解单独考虑了一个反转链表,然后把头尾返回,相当于把这个链表插回去

python 复制代码
class Solution:
    # 翻转一个子链表,并且返回新的头与尾
    def reverse(self, head: ListNode, tail: ListNode):
        prev = tail.next
        p = head
        while prev != tail:
            nex = p.next
            p.next = prev
            prev = p
            p = nex
        return tail, head

    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        hair = ListNode(0)
        hair.next = head
        pre = hair

        while head:
            tail = pre
            # 查看剩余部分长度是否大于等于 k
            for i in range(k):
                tail = tail.next
                if not tail:
                    return hair.next
            nex = tail.next
            head, tail = self.reverse(head, tail)
            # 把子链表重新接回原链表
            pre.next = head
            tail.next = nex
            pre = tail
            head = tail.next
        
        return hair.next

作者:力扣官方题解
链接:https://leetcode.cn/problems/reverse-nodes-in-k-group/solutions/248591/k-ge-yi-zu-fan-zhuan-lian-biao-by-leetcode-solutio/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
天下·第二13 分钟前
使用【docker】简单部署打包构建好的镜像并运行python项目
python·docker·eureka
逆风就重开28 分钟前
大数据开发需要哪些职场知识
大数据·数据仓库·职场和发展·数据分析·职场发展
野老杂谈28 分钟前
【面试系列】Kotlin 高频面试题及详细解答
面试·职场和发展·kotlin·编程语言
左手の明天28 分钟前
【Python网络爬虫案例】python爬虫之模拟登录
开发语言·爬虫·python·模拟登录
华东同舟求职33 分钟前
追觅科技25届校招校招24年社招科技北森题库商业推理综合测评答题攻略、通关技巧
大数据·人工智能·科技·面试·职场和发展·求职招聘
✿ ༺ ོIT技术༻44 分钟前
BFS:队列+树的宽搜
c++·算法·宽度优先
CHNMSCS1 小时前
Django项目 - 合并PDF文件
python·django·pdf
Mac分享吧1 小时前
PyCharm2024 for mac Python编辑开发
python·macos·pycharm·编辑器·mac·软件需求
Itmastergo1 小时前
零基础小白学习 Python,应该如何配置 Python 开发环境?(包含Windows、MacOS、Linux)
开发语言·python·学习
点云侠2 小时前
SARscape——多视滤波
开发语言·人工智能·算法·计算机视觉