当前位置:网站首页>【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
边栏推荐
- Mybatties plus batch warehousing
- How does easyplayer embed a video snapshot into a demo?
- Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo
- Easysearcher cannot find the IP address of easynvr hardware. How to solve it?
- Wwdc21 - App store server API practice summary
- The MIUI 13 development version of Xiaomi mobile phone blocks the chrome application and cannot be opened after installation
- Fetch request details
- An implementation of warning bombing
- TRTC zero foundation -- Video subscription on the code
- Account MFA usage scheme
猜你喜欢
![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]](/img/81/4d3cb059f700dd9243645e64023be7.jpg)
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]

Fetch request details

Encryption related to returnee of national market supervision public service platform

R tree of search tree

Detailed discussion on modular architecture design of MCU firmware

centos7 安装 MySQL 及配置 innodb_ruby

Svn local computer storage configuration
![[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]](/img/f1/972a760459a6d599b5681aa634df09.jpg)
[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]

LRU cache

1058 multiple choice questions (20 points)
随机推荐
Record an edusrc vulnerability mining
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
JS how to delete an item specified in an array
I Arouter framework analysis
What is the difference between the poll () method and the remove () method?
CentOS install redis
The cloud disk has been offline from the internal machine, but the console uninstall failed
What about the high cost of storage system? The original computer room can save so much money!
This point (II)
What should be prepared to develop the company's official website?
[JS reverse hundreds of cases] the login of a HN service network is reverse, and the verification code is null and void
数据交易怎样实现
[advanced Android] kotlin delegate attribute
SwiftUI 组件大全之使用 ScrollView 和 GeometryReader 创建动画 3D卡片 滚动效果
Chapter IV open source projects and deployment
RTOS system selection for charging point software design
How to share small programs released by wechat
[burning] Tencent cloud high tech computing platform HTPC cloud elastic cluster release!
Stress testing with locust on rainbow
Wwdc21 - App store server API practice summary