当前位置:   article > 正文

翻转列表 python_列表类型翻转教学头歌答案

列表类型翻转教学头歌答案

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

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

  1. class ListNode:
  2. def __init__(self, x=None, next=None):
  3. self.val = x
  4. self.next = None
  5. l1 = ListNode(1)
  6. head = l1
  7. for i in range(2, 10):
  8. head.next = ListNode(i)
  9. head = head.next
  10. head = l1
  11. def change(pre, cur):
  12. if cur == None: return pre
  13. cu = cur.next
  14. cur.next = pre
  15. return change(cur, cu)
  16. k = 2
  17. dunmmy = ListNode(0)
  18. pre = dunmmy
  19. end = dunmmy
  20. dunmmy.next = head
  21. while (end.next != None):
  22. start = pre.next
  23. for i in range(k):
  24. end = end.next
  25. if end == None: break
  26. if end == None: break
  27. next1 = end.next
  28. end.next = None
  29. pre.next = change(end.next, start)
  30. start.next = next1
  31. pre = start
  32. end = pre
  33. head = dunmmy.next
  34. while head != None:
  35. print(head.val)
  36. head = head.next

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号