美文网首页
[LinkList]206. Reverse Linked Li

[LinkList]206. Reverse Linked Li

作者: 野生小熊猫 | 来源:发表于2019-02-15 11:09 被阅读0次
  • 分类: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.不知道应该怎么讨论,我觉得还有点晕。。。

相关文章

网友评论

      本文标题:[LinkList]206. Reverse Linked Li

      本文链接:https://www.haomeiwen.com/subject/kprpeqtx.html