LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素

news/2025/3/15 1:13:50/

前言

本文是LeetCode19. Remove Nth Node From End of List解法,这个题目需要删除链表中的倒数第n个位置的元素

代码

# -*- coding: utf-8 -*-# !/usr/bin/env python# Time: 2018/6/27 23:44# Author: sty# File: 19. Remove Nth Node From End of List.py
import jsonclass ListNode:def __init__(self, x):self.val = xself.next = Noneclass Solution:def removeNthFromEnd(self, head, n):""":type head: ListNode:type n: int:rtype: ListNode"""res = ListNode(None)res.next = headcur = runner = resfor _ in range(n):runner = runner.nextwhile runner.next is not None:cur = cur.nextrunner = runner.nextcur.next = cur.next.nextreturn res.nextdef stringToIntegerList(input):return json.loads(input)def stringToListNode(input):# Generate list from the inputnumbers = stringToIntegerList(input)# Now convert that list into linked listdummyRoot = ListNode(0)ptr = dummyRootfor number in numbers:ptr.next = ListNode(number)ptr = ptr.nextptr = dummyRoot.nextreturn ptrdef listNodeToString(node):if not node:return "[]"result = ""while node:result += str(node.val) + ", "node = node.nextreturn "[" + result[:-2] + "]"def main():import sysdef readlines():for line in sys.stdin:yield line.strip('\n')lines = readlines()while True:try:line = next(lines)head = stringToListNode(line);line = next(lines)n = int(line);ret = Solution().removeNthFromEnd(head, n)out = listNodeToString(ret);print(out)except StopIteration:breakif __name__ == '__main__':main()

输入格式

[2,4,5,6,4,5,6,6]
4
[2, 4, 5, 6, 5, 6, 6]

注意

它这里使用的输入方式是列表的形式输入的


http://www.ppmy.cn/news/603923.html

相关文章

使用python建立简单的单链表

代码 import sysclass ListNode:def __init__(self, x):self.val xself.next None# 将列表转换成链表 def list_to_listnode(numbers):dummy_root ListNode(0)ptr dummy_rootfor number in numbers:ptr.next ListNode(number)ptr ptr.nextptr dummy_root.nextreturn pt…

建议使用更加安全的ast.literal_eval去替代eval

前言 如果大家想要在python中将字符串转换成列表,数字,字典等操作,都会想到使用eval(),确实这个函数很好用,但是它却存在一定的安全性 eval的漏洞 如果用户使用如下的代码 open(rD://filename.txt, r).read()__imp…

XNOR-Net解读

XNOR-Net算法详解 XNOR-Net是YOLO的作者作为三作提出的面向计算资源不足的设备如MR眼镜、手机等提出的二进制网络。整篇论文分为两个部分: 1.将卷积核二值化(1,-1)的Binary-Weight-Networks; 2.将输入与卷积核都二值化…

hadoop 添加删除机器以及设置免密登录

添加hadoop机器 先在slaves中添加机器然后启动datanode $: ./usr/hadoop-0.20.2-cdh3u4/bin/hadoop-daemon.sh start datanode查看是否启动 $: jps4696 DataNode 4765 Jps启动tasktrack $: ./usr/hadoop-0.20.2-cdh3u4/bin/hadoop-daemon.sh start tasktracker 查看是否启…

TCP三次握手和四次挥手的解释

基础知识 在TCP层,有个FLAGS字段,这个字段有以下几个标识:SYN, FIN, ACK, PSH, RST, URG. 其中,对于我们日常的分析有用的就是前面的五个字段。 它们的含义是: SYN表示建立连接(synchronous建立联机)FIN表示关闭连…

go未入门学习记录

go未入门学习记录 开发环境配置go1.12.17goland 参考链接macos配置go语言以及goland开发环境 语法学习记录 package main //kaiyu.liushopee.com golang学习笔记 //参考资料 https://www.kancloud.cn/itfanr/go-quick-learn/81636 //参考视频 https://www.bilibili.com/v…

IOS自动化测试环境搭建(Python Java)

一、前言 IOS的App自动化测试与Android的一样,也可以用appium来进行。但是IOS自动化依赖苹果的osx系统、Xcode构建等,且封闭的系统需要苹果开发者账号才可以驱动真机。Appium的环境配置有点麻烦,可能大部分时间都在处理各种稀奇古怪的报错&am…

linux 下根据cpp文件快速书写头文件

假设我们现在有一个hello.cc文件,我们如果想要书写它的头文件hello.h,使用如下的命令即可: cat hello.cc | grep "^\w.*)$" > hello.h 然后我们在hello.h中添加我们依赖的头文件即可