remove the nth node from the end of the list
problem description:
remove the nth node from the end of the list
for example:
given: 1->2->3 n = 1
return: 1->2
thought:
first:you should know the length
second:if the del node is the first node ,just return head.next;esle find the prior node of the del node
third: use a node record the prior node,and make the node.next = del.node.next
last:return head
there is my python solution:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
i = 0
first = head
while first:
i += 1
first = first.next
first=head
if i-n==0:
return head.next
j = 0
while j<i-n-1:
first = first.next
j += 1
second = first
first = first.next
second.next = first.next
return head
it can beat 78% python users
remove the nth node from the end of the list的更多相关文章
- [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 【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 ...
- 19. 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, ...
- No.019: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, ...
- 【leetcode】Remove Nth Node From End of List(easy)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 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, Give ...
- 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [leetcode 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
随机推荐
- 尚学堂马士兵struts2 课堂笔记(一)
06_尚学堂马士兵_Struts2_Struts2_HelloWorld_5 <constant name="struts.devMode" value="true ...
- 从JDK源码角度看java并发的原子性如何保证
JDK源码中,在研究AQS框架时,会发现很多地方都使用了CAS操作,在并发实现中CAS操作必须具备原子性,而且是硬件级别的原子性,java被隔离在硬件之上,明显力不从心,这时为了能直接操作操作系统层面 ...
- UNIX环境高级编程——进程基本概述
一.什么是进程 从用户的角度来看进程是程序的一次执行过程.从操作系统的核心来看,进程是操作系统分配的内存.CPU时间片等资源的基本单位.进程是资源分配的最小单位.每一个进程都有自己独立的地址空间与执行 ...
- python的read() 、readline()、readlines()、xreadlines()
先来一个小例子: import sys dir= os.path.dirname(os.path.abspath(__file__)) file_path='%s/test.txt' % dir f ...
- SQLServer 基础
1当设计表时,对表进行结构性的修改(如将原来可以null的改为不可null),直接改则不允许保存修改,需要选择 工具----选项----designers—表设计器和数据库设计器---阻止保持要求重新 ...
- ARC时代的内存管理
什么是ARC Automatic Reference Counting (ARC) is a compiler feature that provides automatic memory manag ...
- C++中的虚函数表是什么时期建立的?
虚函数表是在什么时期建立的? 最近参加阿里巴巴公司的内推,面试官问了“虚函数表是在什么时期建立的?”.因为以前对虚函数表的理解不够多,所以就根据程序构建(Build)的四个过程(预编译.编译.汇编和链 ...
- C语言关键字static的绝妙用途
为什么要说static妙,它确实是妙,在软件开发或者单片机开发过程中,大家总以为static就是一个静态变量,在变量类型的前面加上就自动清0了,还有就是加上static关键字的,不管是变量还是关键字, ...
- LeetCode之“动态规划”:House Robber && House Robber II
House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...
- Android Camera开发系列(下)——自定义Camera实现拍照查看图片等功能
Android Camera开发系列(下)--自定义Camera实现拍照查看图片等功能 Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 上 ...