题目描述
输入一个链表,反转链表后,输出新链表的表头。
牛客网是没有头结点的,即第一个节点就是有值的
所以
1 class Solution: 2 # 返回ListNode 3 def ReverseList(self, pHead): 4 if pHead==None or pHead.next==None: 5 return pHead 6 p = pHead.next 7 pHead.next=None 8 while p: 9 q = p.next 10 p.next = pHead 11 pHead=p 12 p = q 13 return pHead
假如有头结点:
1 class Solution: 2 # 返回ListNode 3 def ReverseList(self, pHead): 4 # write code here 5 if pHead ==None or pHead.next ==None: 6 return pHead 7 p = pHead.next 8 while p: 9 q = p.next 10 p.next = pHead.next 11 pHead.next=p 12 p = q 13 return pHead.next
2019-12-07 16:22:19