LeetCode - 206. Reverse Linked List

xiaoxiao2021-02-28  96

Q: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both?

A:

# Definition for singly-linked list. class ListNode(object): def __init__(self, x): self.val = x self.next = None class Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ pre = None while head: current = head head = head.next current.next = pre pre = current return pre
转载请注明原文地址: https://www.6miu.com/read-61017.html

最新回复(0)