当前位置:   article > 正文

Python | Leetcode Python题解之第206题反转链表

Python | Leetcode Python题解之第206题反转链表

题目:

题解:

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, val=0, next=None):
  4. # self.val = val
  5. # self.next = next
  6. class Solution:
  7. def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
  8. newHead = ListNode(0)
  9. newHead.next = None
  10. h = head
  11. while h:
  12. cur = ListNode(h.val)
  13. cur.next = newHead.next
  14. newHead.next = cur
  15. h = h.next
  16. return newHead.next
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/786158
推荐阅读
相关标签
  

闽ICP备14008679号