leetcode-92-反转链表②
生活随笔
收集整理的這篇文章主要介紹了
leetcode-92-反转链表②
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述:
方法一:
class Solution:def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:dummy = ListNode(0)dummy.next = headpre = dummyfor i in range(m-1):pre = pre.nextstart = pre.nexttrail = start.nextfor i in range(n-m):start.next = trail.nexttrail.next = pre.nextpre.next = trailtrail = start.nextreturn dummy.next?方法二:
找到要翻轉部分的鏈表,將其翻轉,再與原鏈表拼接;
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:dummy = ListNode(0)dummy.next = headpre = dummyfor i in range(m-1):pre = pre.nextnode =Nonecur = pre.nextfor i in range(n-m+1):tmp = cur.nextcur.next = nodenode = curcur = tmppre.next.next = curpre.next = nodereturn dummy.next?
轉載于:https://www.cnblogs.com/oldby/p/11180942.html
總結
以上是生活随笔為你收集整理的leetcode-92-反转链表②的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《SpringMVC从入门到放肆》三、D
- 下一篇: 【ABAP系列】SAP 面试 ABAPe