当前位置:网站首页>[leetcode] flip linked list II
[leetcode] flip linked list II
2022-06-23 03:38:00 【LawsonAbs】
1 subject
Advanced version of linked list turnover , That is, only a part of the linked list is flipped . This requires us to find the head and tail nodes of the list to be flipped , Then turn it over again .
2 thought
- Flip position left To Location right The linked list node of
- This task is divided into two parts
- determine pre_head, tail_next 2. List flip
3 Code
# 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. determine 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 # Flip from the first header node
else:
start = pre_head.next
# step 2. List flip
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
边栏推荐
- 【LeetCode】179. 最大数
- Build information query applet by using micro build
- ABCD identifier of SAP mm initial cycle count
- Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]
- Even if you don't learn gradle, these common development operations are worth mastering
- What is the difference between ArrayList and LinkedList?
- Cross tenant data integration link connection
- Regeorg actual attack and defense
- Initialize MySQL Gorm through yaml file
- One of the touchdesigner uses - Download and install
猜你喜欢

One of the touchdesigner uses - Download and install

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
![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]

【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行

嵌入式软件测试工具TPT18更新全解析

聊聊内存模型和内存序

centos7 安装 MySQL 及配置 innodb_ruby

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

Jmeter- (V) simulated user concurrent login for interface test

直接插入排序
随机推荐
Chapter V kubernetes quick start
Customization of openfeign
2022-01-22: Li Kou 411, the abbreviation of the shortest exclusive word. Give a string number
Salesforce heroku (V) application in salesforce (canvasapp)
What about the high cost of storage system? The original computer room can save so much money!
d重载嵌套函数
Detailed discussion on modular architecture design of MCU firmware
第一批00后下场求职:不要误读他们的“不一样”
Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]
What is the difference between ArrayList and LinkedList?
WordPress modifying fixed links and pseudo statics
Form submit onclick and onsubmit
Best practices for building multi architecture mirrors
January 17, 2022: word rule II. Give you a pattern and a character
Regeorg actual attack and defense
Insérer le tri directement
Downloading sqlserver versions (2016-2019)
TRTC setaudioroute invalid problem
直接插入排序
Goframe framework (RK boot): enable tls/ssl