Leetcode Day8 3
剑指 Offer 24. 反转链表
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
双指针暴力解了
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
cur=head
pre=None
while cur:
tmp=cur.next
cur.next=pre
pre=cur
cur=tmp
return pre

看看大佬们怎么做的。。
递归
但是感觉这种做法没意义啊,就先不管了