当前位置:网站首页>【LeetCode】19-删除链表的倒数第N个结点
【LeetCode】19-删除链表的倒数第N个结点
2022-07-02 12:09:00 【酥酥~】
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
示例 1:
输入: head = [1,2,3,4,5], n = 2
输出: [1,2,3,5]
示例 2:
输入: head = [1], n = 1
输出: []
示例 3:
输入: head = [1,2], n = 1
输出: [1]
提示:
- 链表中结点的数目为 sz
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= sz
进阶:你能尝试使用一趟扫描实现吗?
#链表存入数列中随机访问
# 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)#构造一个带头结点的链表
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
#入栈
# 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
#双指针。进阶:一趟扫描
# 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#即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
边栏推荐
- MD5 encryption
- 14_ Redis_ Optimistic lock
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- 损失函数与正负样本分配:YOLO系列
- 终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
- Map introduction
- 10_ Redis_ geospatial_ command
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- LeetCode_ String_ Simple_ 412.Fizz Buzz
- 语义分割学习笔记(一)
猜你喜欢
Custom exception
损失函数与正负样本分配:YOLO系列
Bing. Com website
How does the computer set up speakers to play microphone sound
05_ queue
JVM architecture, classloader, parental delegation mechanism
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
XML Configuration File
02_线性表_顺序表
LeetCode刷题——去除重复字母#316#Medium
随机推荐
损失函数与正负样本分配:YOLO系列
MySQL calculate n-day retention rate
Custom exception
21_Redis_浅析Redis缓存穿透和雪崩
15_ Redis_ Redis. Conf detailed explanation
LeetCode刷题——奇偶链表#328#Medium
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
SQL stored procedure
Evaluation of embedded rz/g2l processor core board and development board of Feiling
How to write sensor data into computer database
4. Jctree related knowledge learning
Yolov5 code reproduction and server operation
Tidb hybrid deployment topology
MD5加密
. Solution to the problem of Chinese garbled code when net core reads files
07_ Hash
MySQL -- Index Optimization -- order by
PHP method to get the index value of the array item with the largest key value in the array
folium地图无法显示的问题,临时性解决方案如下
4. Data splitting of Flink real-time project