[LinkList]206. Reverse Linked Li

2019-02-15  本文已影响0人  野生小熊猫

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.不知道应该怎么讨论,我觉得还有点晕。。。

上一篇 下一篇

猜你喜欢

热点阅读