leetcode-python-反转链表

第一轮解法:

1)双指针,先给定尾部为空,然后逐个出栈

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        tail = None
        temp = head
        while temp:
            temp = head.next
            head.next = tail
            tail = head
            head = temp
            # tail.PrintListNode()
        return tail

 

posted @ 2021-06-02 16:08  泊鸽  阅读(52)  评论(0)    收藏  举报