Last active
July 5, 2020 12:18
-
-
Save asyncins/51b89468f30e0bcc68410e9cc1b4d8cc to your computer and use it in GitHub Desktop.
[算法-单链表反转]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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