當(dāng)前位置:
首頁 >
文巾解题 206. 反转链表
發(fā)布時間:2025/4/5
20
豆豆
生活随笔
收集整理的這篇文章主要介紹了
文巾解题 206. 反转链表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 題目描述
2 解題思路
2.1 創(chuàng)建輔助鏈表
# 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: ListNode) -> ListNode:if(head==None):return Nonetmp=ListNode(head.val)ret=ListNode(head.val)while(head.next):head=head.nextret=ListNode(head.val)ret.next=tmptmp=retreturn ret2.2
# 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: ListNode) -> ListNode:if(head==None):return Noneprev=Nonecur=headwhile(cur!=None):temp=cur.next #暫存下一個節(jié)點cur.next=prev #使當(dāng)前索引到的節(jié)點指向之前已經(jīng)倒轉(zhuǎn)好的鏈表prev=cur#倒轉(zhuǎn)的鏈表向后進一位cur=temp # 未索引的列表向后退一位return prev總結(jié)
以上是生活随笔為你收集整理的文巾解题 206. 反转链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NTU 21fall-CE 7454(d
- 下一篇: 交通预测论文笔记《Attention B