当前位置:网站首页>[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
边栏推荐
- 21_Redis_浅析Redis缓存穿透和雪崩
- [solution] educational codeforces round 82
- Semantic segmentation learning notes (1)
- Engineer evaluation | rk3568 development board hands-on test
- 12_Redis_Bitmap_命令
- 10_ Redis_ geospatial_ command
- 【LeetCode】19-删除链表的倒数第N个结点
- 12_ Redis_ Bitmap_ command
- 士官类学校名录
- 11_ Redis_ Hyperloglog_ command
猜你喜欢

I made an istio workshop. This is the first introduction

Case introduction and problem analysis of microservice

04_ 栈

Loss function and positive and negative sample allocation: Yolo series

6.12 critical moment of Unified Process Platform

Leetcode skimming - remove duplicate letters 316 medium

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测

Steps for Navicat to create a new database

10_Redis_geospatial_命令

LeetCode刷题——验证二叉树的前序序列化#331#Medium
随机推荐
17_Redis_Redis发布订阅
12_ Redis_ Bitmap_ command
损失函数与正负样本分配:YOLO系列
11_ Redis_ Hyperloglog_ command
【LeetCode】19-删除链表的倒数第N个结点
【LeetCode】283-移动零
【LeetCode】1162-地图分析
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
NBA player analysis
18_ Redis_ Redis master-slave replication & cluster building
夏季高考文化成绩一分一段表
4. Jctree related knowledge learning
MySQL calculate n-day retention rate
搭建自己的语义分割平台deeplabV3+
03_线性表_链表
5. Practice: jctree implements the annotation processor at compile time
Bing.com網站
【LeetCode】486-预测赢家
Case introduction and problem analysis of microservice
【LeetCode】189-轮转数组