206. Reverse Linked List

206. Reverse Linked List

题目:
https://leetcode.com/problems/reverse-linked-list/

难度:
Easy

用三个指针,分别指向prev,cur 和 nxt,然后loop一圈还算比较简单.

待尝试递归.

# 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
        """
        prev = None 
        cur = head
        while(cur):
            nxt = cur.next
            cur.next = prev
            prev = cur
            cur = nxt
        return prev
    原文作者:oo上海
    原文地址: https://www.jianshu.com/p/05c36f0bf0c2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞