https://leetcode.com/problems/reverse-linked-list/
你會得到一個鏈結串列,把它反過來吧
今天是很基本的題目,是資訊工程系大一就會學到的東西,所以今天就用超新手向的方式來寫這篇吧!
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
current = head
previous = None
while current:
temp = current.next
current.next = previous
previous = current
current = temp
return previous
temp = current.next
current.next = previous
previous = current
current = temp
看文字說明覺得不夠的人,就看看下面連結裡的動畫吧
動畫連結