Last updated 2 years ago
反转链表_牛客题霸_牛客网
问题简述
给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
思路
详见代码
class Solution: def ReverseList(self , head: ListNode) -> ListNode: pre, cur = None, head while cur: nxt = cur.next cur.next = pre pre = cur cur = nxt return pre