栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

翻转列表 python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

翻转列表 python

太难啦,!!!昨天晚上,不,昨天上午开始看,一直到今天下午,,,终于在参考别人图解的基础上,调出来了!!感谢这个图

下面是我的代码,和参考答案大同小异吧,包括了链表定义和赋值

class ListNode:
    def __init__(self, x=None, next=None):
        self.val = x
        self.next = None

l1 = ListNode(1)
head = l1
for i in range(2, 10):
    head.next = ListNode(i)
    head = head.next
head = l1

def change(pre, cur):
    if cur == None: return pre
    cu = cur.next
    cur.next = pre
    return change(cur, cu)

k = 2
dunmmy = ListNode(0)
pre = dunmmy
end = dunmmy
dunmmy.next = head
while (end.next != None):
    start = pre.next
    for i in range(k):
        end = end.next
        if end == None: break
    if end == None: break
    next1 = end.next
    end.next = None
    pre.next = change(end.next, start)
    start.next = next1
    pre = start
    end = pre
head = dunmmy.next
while head != None:
    print(head.val)
    head = head.next

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/889772.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号