1 反转链表python



解答:
class ListNode:
    def __init__(self,x):
        self.val=x
        self.next=None
class Solution:
    #返回ListNode
    def ReverseList(self , pHead):
        #指向头节点,头节点是有值的
        head=ListNode(-1) #创建一个新链表
        while pHead is not None:#pHead为NULL表示为空表
            tmp=head.next       #连接在新表上的元素
            head.next=pHead     #head连接旧表
            pHead=pHead.next    #旧表向前移动
            head.next.next=tmp  #head新表连接旧表倒着的第二个元素
        return head.next #返回新表中的元素
过程:


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号