题目链接:206.反转链表
难度:简单
描述:
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
提示:
链表中的结点数是[0, 5000]。
题解
思路:
没什么好说的。
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution:def reverseList(self, head: ListNode) -> ListNode:prev = Nonecur = headwhile cur is not None:temp = cur.nextcur.next = prevprev = curcur = tempreturn prev
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution:def reverseList(self, head: ListNode) -> ListNode:if head is None or head.next is None:return head# 找到最后一个有效节点ret = self.reverseList(head.next)head.next.next = headhead.next = Nonereturn ret
