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,
- Given linked list: 1->2->3->4->5, and n = 2.
- After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
代码:
从链表中删除从右向左数第N个元素,况且只能遍历链表一遍。
所以,需要定义两个指针,相差是N个元素,同时向前遍历,当靠前的元素走到最后的时候,在后面的元素刚好与最后一个元素距离为N,删除即可。
于是:
- #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
- """
- p = head
- q = head
- if head.next ==None:return []
- for i in range(0,n):
- q = q.next
- if not q : return head.next
- while q.next:
- p = p.next
- q = q.next
- temp = p.next.next
- p.next = temp
- return head
- if __name__=='__main__':
- arr = [1,2]
- p = ListNode(arr[0])
- head = p
- for i in arr[1:]:
- p.next = ListNode(i)
- p = p.next
- s=Solution()
- q=s.removeNthFromEnd(head,2)
- while q:
- print (q.val)
- q = q.next
19. Remove Nth Node From End of List的更多相关文章
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- [LeetCode] 19. 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 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, ...
- Java [leetcode 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 ...
- Leetcode 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, Give ...
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
随机推荐
- Matlab2015矩阵表示03
1. 矩阵表示 >>行元素分隔: 空格'space'或逗号',' >>列分隔: 分号或回车换行符 2. 冒号表达式 1) start:end 2) start: step : ...
- 软件工程(FZU2015)赛季得分榜,第一回合
目录 第一回合 第二回合 第三回合 第四回合 第五回合 第6回合 第7回合 第8回合 第9回合 第10回合 第11回合 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分 ...
- Delphi 实现数字转大写
从网上找的一段代码 /// <summary> /// 小写转大写 /// </summary> /// <param name="mmje"> ...
- PHPExcel读取excel文件
<?php set_time_limit(0); $dir = dirname(__FILE__);//当前脚本所在路径 require $dir."/PHPExcel_1.8.0/C ...
- 解决:Error: JAVA_HOME is not defined correctly
问题重现: Error: JAVA_HOME is not defined correctly. We cannot execute :/usr/lib/jvm/java-7-oracle 问题分析: ...
- CentOS 6.7下利用Rsyslog+LogAnalyzer+MySQL部署日志服务器
一.简介 LogAnalyzer 是一款syslog日志和其他网络事件数据的Web前端.它提供了对日志的简单浏览.搜索.基本分析和一些图表报告的功能.数据可以从数据库或一般的syslog文本文件中获取 ...
- Java面试之SpringMVC总结以及在面试中的一些问题.
1.简单的谈一下SpringMVC的工作流程? 流程 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理 ...
- 手机端多种分享plugin插件地址
//qq cordova plugin add https://github.com/iVanPan/Cordova_QQ.git --variable QQ_APP_ID=app_id 参考文档 h ...
- 点评前端开发工具cortex安装使用方法
cortex安装方法: 安装最新版 sudo npm install -g cortex cortex config set registry http://registry.cortexjs.org ...
- Java学习资料
微信扫码:http://v.dxsbb.com/jisuanji/Java之家:http://www.javazhijia.com/bs/biye/137.html一些 http://www.ibei ...