206. 反转链表

流过昼夜 提交于 2020-03-02 17:51:17

1、解题思路

2、代码

【图源Leetcode 206 题解   侵删】

Leetcode-206-题解

# 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:
        pre = None
        cur = head
        while cur is not None:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre

Python能一句话说完的绝不两句【人生苦短,我用Python】 

# 人生苦短,我用Python

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        pre, cur = None, head
        while cur is not None:
            cur.next, pre, cur = pre, cur, cur.next
        return pre


 

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