【leetcode】206. 反转链表
题目
206. 反转链表
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
代码
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):def reverseList(self, head):""":type head: Optional[ListNode]:rtype: Optional[ListNode]""""""1.遍历,翻转2.遍历,一直插入0位置"""pre = Nonecur = headwhile cur:nxt = cur.nextcur.next = prepre = curcur = nxtreturn pre