当前位置:网站首页>[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
边栏推荐
猜你喜欢

Semantic segmentation learning notes (1)

Data analysis thinking analysis methods and business knowledge - business indicators

Solve the problem of frequent interruption of mobaxterm remote connection

JVM architecture, classloader, parental delegation mechanism

14_Redis_乐观锁

LeetCode刷题——去除重复字母#316#Medium

【LeetCode】1254-统计封闭岛屿的数量

Yolo format data set processing (XML to txt)

5. Practice: jctree implements the annotation processor at compile time

2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
随机推荐
. Net again! Happy 20th birthday
Bing.com網站
Semantic segmentation learning notes (1)
List set & UML diagram
19_Redis_宕机后手动配置主机
20_Redis_哨兵模式
Beijing rental data analysis
Yolov5 code reproduction and server operation
Pytoch saves tensor to Mat file
18_Redis_Redis主从复制&&集群搭建
19_ Redis_ Manually configure the host after downtime
Force deduction solution summary 2029 stone game IX
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
密码学基础知识
21_ Redis_ Analysis of redis cache penetration and avalanche
【LeetCode】977-有序數組的平方
Evaluation of embedded rz/g2l processor core board and development board of Feiling
MySQL calculate n-day retention rate
6.12 critical moment of Unified Process Platform
PTA 天梯赛习题集 L2-001 城市间紧急救援