python 链表反转

    xiaoxiao2023-10-12  191

    图解链表反转

    python实现1

    头插法

    class Solution: def reverseList(self, head: ListNode) -> ListNode: if head==None or head.next==None: return head p= head q = head.next p.next = None while q: r = q.next q.next = p p = q q = r return p

     python实现2

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ p=None while head: temp=head head=head.next temp.next=p p=temp return p

    https://blog.csdn.net/feliciafay/article/details/6841115   

    最新回复(0)