LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素
前言
本文是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]
注意
它这里使用的输入方式是列表的形式输入的
LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素相关推荐
- java中删除node节点_[Java]LeetCode237. 删除链表中的节点 | Delete Node in a Linked List
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Remove Nth Node From End of List - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Remove Nth Node From End of List - LeetCode 注意点 考虑删除的结点是开头的那个结点 输入给的链表是没有开头的& ...
- LeetCode算法入门- Remove Nth Node From End of List -day17
LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释: Given a linked list, remove the n-th nod ...
- LeetCode:Remove Nth Node From End of List 移除链表倒第n项
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...
- LeetCode 19. Remove Nth Node From End of List
LeetCode 19. Remove Nth Node From End of List Solution1:我的答案 并不算是最优解法. /*** Definition for singly-li ...
- 数据结构与算法 | Leetcode 19. Remove Nth Node From End of List
原文链接:https://wangwei.one/posts/jav... 前面,我们实现了 两个有序链表的合并 操作,本篇来聊聊,如何删除一个链表的倒数第N个节点. 删除单链表倒数第N个节点 Lee ...
- [Swift]LeetCode19. 删除链表的倒数第N个节点 | Remove Nth Node From End of List
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- LeetCode:Remove Nth Node From End of List
题目链接 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- Remove Nth Node From End of List leetcode java
题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...
最新文章
- python代码计算矩形面积_学习资料Python语言基础知识笔记以及答案
- 神在夏至祭降下了神谕(oracle)
- centos 并发请求数_Linux Shell多进程并发以及并发数控制
- spring框架学习(三)junit单元测试
- 画一个圆角多边形_CAD零基础教程,矩形和多边形的画法
- cli3解决 ie11语法错误 vue_使用 VueCLI 3.x 快速搭建Vue + TS + Kbone + KboneUI + 云开发 项目...
- 上下伸缩代码_CQRS之旅——旅程4(扩展和增强订单和注册限界上下文)
- 蔡崇信与马云的 20 年
- 20191022:(leetcode习题)山脉数组的峰顶索引
- 在UI程序设计中使用BackgroundWorker进行多线程异步处理
- 计算机网络(二)—— 物理层(1、2、3):物理层的基本概念、物理层的下面的传输媒体、传输方式
- 免费php文件加密软件,php源码加密 在线加密工具
- 区别python中list()和tolist()的区别
- CodeForces 140C New Year Snowm
- 至于你信不信(由你),我反正信了
- Linux就业技术指导(四):企业CDN缓存加速原理解密
- 【原型模式】原型模式深入分析
- 人工智能快速发展 计算机视觉产品打造智能社会“入口”
- 东北大学计算机a类吗,东北大学在985大学中水平怎么样?东北大学2020能回归双一流A类吗?...
- 实验二、数据库的建立和维护
热门文章
- 2022-2028年中国汽车修理行业市场前瞻与投资规划分析报告
- php错误提示如何查询,php-如何显示我的MySQLi查询错误?
- Aho-Corasick 多模式匹配算法(AC自动机) 的算法详解及具体实现
- 4 用python进行OpenCV实战之图像变换1(平移)
- 使用阿里云服务器安装docker,并用nginx示例
- tf.placeholder函数说明
- LeetCode中等题之删除链表的中间节点
- 单精度和半精度混合训练
- 【CV】吴恩达机器学习课程笔记第10章
- 石头机器人拖地水量调节_石头扫地机器人T7上手体验:电控水箱和超大容量,扫拖一体全能型...