- 分类:LinkList
- 时间复杂度: O(n)
- 空间复杂度: O(n)
206. Reverse Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
代码:
# 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':
res=None
if head==None:
return res
current=head
next_=current.next
while(current!=None):
current.next=res
res=current
current=next_
if current!=None:
next_=next_.next
return res
讨论:
1.不知道应该怎么讨论,我觉得还有点晕。。。
网友评论