欢迎光临散文网 会员登陆 & 注册

Leetcode Day8 3

2022-04-09 21:12 作者:我喜欢喝一点点  | 我要投稿

剑指 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



看看大佬们怎么做的。。

递归

但是感觉这种做法没意义啊,就先不管了

Leetcode Day8 3的评论 (共 条)

分享到微博请遵守国家法律