当前位置: 首页 > news >正文

力扣刷题-热题100题-第31题(c++、python)

25. K 个一组翻转链表 - 力扣(LeetCode)https://leetcode.cn/problems/reverse-nodes-in-k-group/?envType=study-plan-v2&envId=top-100-liked

常规模拟

根据翻转的长度找到头和尾,进入函数进行翻转

主程序里有循环不断找到头和尾并拼接起来

//c++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    pair<ListNode*,ListNode*> r(ListNode* head,ListNode* tail)
    {
        ListNode* pre=tail->next;
        ListNode* a=head;
        while(pre!=tail)
        {
            ListNode* nex=a->next;
            a->next=pre;
            pre=a;
            a=nex;
        }
        return {tail,head};
    }
    ListNode* reverseKGroup(ListNode* head, int k) 
    {
        ListNode* h=new ListNode(0);
        ListNode* pre=h;
        h->next=head;
        while(head)
        {
            ListNode* tail=pre;
            for(int i=0;i<k;i++)
            {
                tail=tail->next;
                if(!tail)    return h->next;
            }
            ListNode* n=tail->next;
            tie(head,tail)=r(head,tail);
            pre->next=head;
            tail->next=n;
            pre=tail;
            head=tail->next;
        }
        return h->next;
    }
};

#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 r(hh:ListNode,tail:ListNode):
            nex=tail.next
            h1=hh
            while nex!=tail:
                h2=h1.next
                h1.next=nex
                nex=h1
                h1=h2
            return tail,hh

        ans=ListNode()
        ans.next=head
        pre=ans
        while pre.next:
            tail=pre
            for i in range(k):
                tail=tail.next
                if tail==None:
                    return ans.next
            a=tail.next
            aa,bb=r(pre.next,tail)
            pre.next=aa
            bb.next=a
            pre=bb
        return ans.next

这里是我的第一想法,比较愚蠢,直接用内存把地址存起来,然后倒序遍历去拼接,一点参考

#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]:
        ans=ListNode()
        pre=ans
        h=head
        while head:
            a=[]
            for i in range(k):
                if h==None:
                    return ans.next
                a.append(h)
                h=h.next
            a.append(h)
            for i in range(-2,-k-2,-1):
                pre.next=a[i]
                pre=pre.next
            pre.next=a[-1]
        return ans.next

http://www.dtcms.com/a/113716.html

相关文章:

  • 博途 TIA Portal之1200做主站与200SMART的S7通讯
  • 《减压宝典》Python篇
  • leetcode每日一题:替换子串得到平衡字符串
  • vue3实现markdown预览和编辑
  • Cursor 无限续杯 Windows版
  • 智能体开发实战指南:提示词设计、开发框架与工作流详解
  • ROS多设备交互
  • 用C语言控制键盘上的方向键
  • LightRAG核心原理和数据流
  • Cisco Packet Tracer 8.0(新版)
  • 【神经网络】python实现神经网络(三)——正向学习的模拟演练
  • Unity插件SuperScrollView详解(进阶篇)
  • MySQL篇(五)MySQL主从同步原理深度剖析
  • 面试算法高频03-递归
  • day 8 TIM定时器
  • 第八章 Python基础进阶-数据可视化(终)
  • FfreeRTOS有阻塞作用的API
  • 12款字重国外法国风格复古报纸日历设计衬线英文字体安装包 Claire Font Family
  • docker swarm常用命令
  • python爬虫爬取淘宝热销(热门)男装商品信息(课程设计;提供源码、使用说明文档及相关文档;售后可联系博主)
  • Rust切片、结构体、枚举
  • macOS下SourceInsight的替代品
  • 前端工程化之模块化开发 webpack
  • 完整的Python程序,它能够根据两个Excel表格(假设在同一个Excel文件的不同sheet中)中的历史数据来预测未来G列数字
  • #C8# UVM中的factory机制 #S8.1.1# 多态的实现方式(三)
  • LeetCode-98. 验证二叉搜索树
  • java流程控制06:While循环
  • HeidiSQL:多数据库管理工具
  • LeeCode题库第1695题
  • 架构下的按钮效果设置