当前位置:网站首页>[leetcode] 19 delete the penultimate node of the linked list
[leetcode] 19 delete the penultimate node of the linked list
2022-07-02 15:36:00 【Crisp ~】
I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list .
Example 1:
Input : head = [1,2,3,4,5], n = 2
Output : [1,2,3,5]
Example 2:
Input : head = [1], n = 1
Output : []
Example 3:
Input : head = [1,2], n = 1
Output : [1]
Tips :
- The number of nodes in the list is sz
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= sz
Advanced : Can you try a scan implementation ?
# The linked list is stored in the sequence for random access
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeNthFromEnd(self, head, n):
arr = []
length = 0
new = ListNode(0,head)# Construct a linked list of leading nodes
tmp = new
while tmp:
arr.append(tmp)
tmp = tmp.next
length += 1
arr[length-n-1].next = arr[length-n-1].next.next
return new.next
# Push
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeNthFromEnd(self, head, n):
arr = []
new = ListNode(0,head)
tmp = new
while tmp:
arr.append(tmp)
tmp = tmp.next
for i in range(n):
arr.pop()
pre = arr.pop()
pre.next = pre.next.next
return new.next
# Double pointer . Advanced : A scan
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeNthFromEnd(self, head, n):
new = ListNode(0,head)
first = new
second = head# namely new.next
for i in range(n):
second = second.next
while second:
first = first.next
second = second.next
first.next = first.next.next
return new.next
边栏推荐
- 18_ Redis_ Redis master-slave replication & cluster building
- QML pop-up frame, customizable
- 13_Redis_事务
- Leetcode question brushing - parity linked list 328 medium
- FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
- 党史纪实主题公益数字文创产品正式上线
- 20_Redis_哨兵模式
- 05_ queue
- Infra11199 database system
- 自定义异常
猜你喜欢

Engineer evaluation | rk3568 development board hands-on test

工程师评测 | RK3568开发板上手测试

Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July

MySQL calculate n-day retention rate

16_Redis_Redis持久化

10_ Redis_ geospatial_ command

Leetcode skimming -- count the number of numbers with different numbers 357 medium

自定义异常

03_線性錶_鏈錶

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
随机推荐
【LeetCode】1140-石子游戏II
12_ Redis_ Bitmap_ command
LeetCode刷题——验证二叉树的前序序列化#331#Medium
yolo格式数据集处理(xml转txt)
Evaluation of embedded rz/g2l processor core board and development board of Feiling
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
MySQL -- Index Optimization -- order by
folium地图无法显示的问题,临时性解决方案如下
QML pop-up frame, customizable
21_ Redis_ Analysis of redis cache penetration and avalanche
. Solution to the problem of Chinese garbled code when net core reads files
How to write sensor data into computer database
13_Redis_事务
17_ Redis_ Redis publish subscription
【LeetCode】877-石子游戏
Redux - detailed explanation
【LeetCode】19-删除链表的倒数第N个结点
NBA player analysis
13_ Redis_ affair