Skip to content

Instantly share code, notes, and snippets.

@asyncins
Last active July 5, 2020 12:18
Show Gist options
  • Save asyncins/51b89468f30e0bcc68410e9cc1b4d8cc to your computer and use it in GitHub Desktop.
Save asyncins/51b89468f30e0bcc68410e9cc1b4d8cc to your computer and use it in GitHub Desktop.
[算法-单链表反转]
# 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:
prev = None
current = head
# 遍历链表
while current:
# 记录当前结点的下一结点,同时将当前结点指向 prev
tmp, current.next = current.next, prev
# prev 和 current 结点都前进一位
prev, current = current, tmp
return prev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment