当前位置:网站首页>[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
边栏推荐
- 语义分割学习笔记(一)
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- How to find a sense of career direction
- SQL stored procedure
- 05_ queue
- . Solution to the problem of Chinese garbled code when net core reads files
- Infra11199 database system
- 【LeetCode】19-删除链表的倒数第N个结点
- JVM architecture, classloader, parental delegation mechanism
- 03. Preliminary use of golang
猜你喜欢
yolo格式数据集处理(xml转txt)
19_Redis_宕机后手动配置主机
Redux - detailed explanation
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
MySQL calculate n-day retention rate
. Solution to the problem of Chinese garbled code when net core reads files
SQL transaction
04_ 栈
工程师评测 | RK3568开发板上手测试
Wechat Alipay account system and payment interface business process
随机推荐
Steps for Navicat to create a new database
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
【LeetCode】344-反转字符串
13_Redis_事务
How to intercept the value of a key from the JSON string returned by wechat?
【LeetCode】877-石子游戏
19_Redis_宕机后手动配置主机
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
[leetcode] 977 square of ordered array
Loss function and positive and negative sample allocation: Yolo series
02_线性表_顺序表
15_Redis_Redis.conf详解
怎样从微信返回的json字符串中截取某个key的值?
Download blender on Alibaba cloud image station
08_ strand
损失函数与正负样本分配:YOLO系列
17_ Redis_ Redis publish subscription
工程师评测 | RK3568开发板上手测试
Leetcode skimming -- sum of two integers 371 medium
Redux - detailed explanation