当前位置:网站首页>【LeetCode】翻转链表II
【LeetCode】翻转链表II
2022-06-23 03:32:00 【LawsonAbs】
1 题目
链表翻转的进阶版,即只翻转链表的某一部分。这就要求我们找到待翻转链表的头结点和尾节点,然后再翻转一下就可以了。
2 思想
- 翻转位置left 到 位置right 的链表节点
- 本任务拆解成两部分
- 确定pre_head, tail_next 2. 链表翻转
3 代码
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
pre_head = None
tmp = head
# step 1. 确定pre_head
cnt = 1
while(cnt < left):
pre_head = tmp
tmp = tmp.next
cnt += 1
cnt = 1
tail_next = head
while(cnt < right):
tail_next = tail_next.next
cnt +=1
tail_next = tail_next.next
if pre_head is None:
start = head # 从第一个头结点开始翻转
else:
start = pre_head.next
# step 2. 链表翻转
nxt = start.next
tmp = start
while(start and nxt != tail_next):
tmp2 = nxt.next #
nxt.next = start
start = nxt
nxt = tmp2
if pre_head is None:
head = start
else:
pre_head.next = start
tmp.next = tail_next
return head
边栏推荐
- 数据交易怎样实现
- Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph
- Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]
- CFS topics
- Easygbs solution to video playback failure caused by tcp/udp configuration problems
- Tachometer script
- JS how to delete an item specified in an array
- Troubleshooting and resolution of asydss virtual live broadcast status synchronization and service downtime
- About SSL certificates
- Summary of some precautions and problems in the use of tars framework (mengxinxiang)
猜你喜欢
![Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]](/img/de/d73805aaf4345ca3d2a7baf85aab8d.jpg)
Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]

Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph

Static code block, code block, constructor execution order

mysql 数据恢复 (.ibdata1, bin log)

Gakataka student end to bundle Version (made by likewendy)

【二分】leetcode1011. Capacity To Ship Packages Within D Days

新版kali切换最高账户

Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e

Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)
![Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]](/img/4b/dd272f98b89a157180bf68570d2763.jpg)
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
随机推荐
How does easyplayer embed a video snapshot into a demo?
1 approved, edit again
Generate PDF417 code in batch through TXT file
To implement a task scheduling system, it is enough to read this article
What is the difference between JS undefined and null
LRU cache
How to print multiple barcode labels on one sheet of paper
How to install redis version 5.0.8 on the pagoda panel
Easygbs service is killed because the user redis is infected with the mining virus process. How to solve and prevent it?
Mybatties plus batch warehousing
Cross tenant data integration link connection
Why not use math random() ?!
[burning] Tencent cloud high tech computing platform HTPC cloud elastic cluster release!
Easysearcher cannot find the IP address of easynvr hardware. How to solve it?
Parsing the implementation of easygbs compatible token as parameter passing
Communication between containers flannel and calico comparison
Firewall and IP security policy configuration
On the way home from the Spring Festival transportation, traffic visualization will escort you
C. Differential Sorting
Drill down into handler, looper, messagequeue