Leetcode 206 Reverse Linked List

南楼画角 提交于 2019-11-29 22:19:59

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
# 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:
        if head == None:
            return None
        # L  M  R
        # M -> L
        L = None
        M = None
        R = head
        while R.next != None:
            L = M
            M = R
            R = R.next
            M.next = L
        R.next = M
        return R

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!