题目:

Given a linked list, remove the nth node from the end of list and return its head.

For example,

  1. Given linked list: 1->2->3->4->5, and n = 2.
  2.  
  3. 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,删除即可。

于是:

  1. #Definition for singly-linked list.
  2. class ListNode(object):
  3. def __init__(self, x):
  4. self.val = x
  5. self.next = None
  6.  
  7. class Solution(object):
  8.  
  9. def removeNthFromEnd(self, head, n):
  10. """
  11. :type head: ListNode
  12. :type n: int
  13. :rtype: ListNode
  14. """
  15. p = head
  16. q = head
  17. if head.next ==None:return []
  18. for i in range(0,n):
  19. q = q.next
  20. if not q : return head.next
  21. while q.next:
  22. p = p.next
  23. q = q.next
  24. temp = p.next.next
  25. p.next = temp
  26. return head
  27.  
  28. if __name__=='__main__':
  29. arr = [1,2]
  30. p = ListNode(arr[0])
  31. head = p
  32. for i in arr[1:]:
  33. p.next = ListNode(i)
  34. p = p.next
  35. s=Solution()
  36. q=s.removeNthFromEnd(head,2)
  37. while q:
  38. print (q.val)
  39. q = q.next

19. Remove Nth Node From End of List的更多相关文章

  1. 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. ...

  2. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  3. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  4. 【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 ...

  5. [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 ...

  6. [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, ...

  7. 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 ...

  8. 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 ...

  9. 【一天一道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 ...

随机推荐

  1. Matlab2015矩阵表示03

    1. 矩阵表示 >>行元素分隔: 空格'space'或逗号',' >>列分隔: 分号或回车换行符 2. 冒号表达式 1) start:end 2) start: step : ...

  2. 软件工程(FZU2015)赛季得分榜,第一回合

    目录 第一回合 第二回合 第三回合 第四回合 第五回合 第6回合 第7回合 第8回合 第9回合 第10回合 第11回合 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分 ...

  3. Delphi 实现数字转大写

    从网上找的一段代码 /// <summary> /// 小写转大写 /// </summary> /// <param name="mmje"> ...

  4. PHPExcel读取excel文件

    <?php set_time_limit(0); $dir = dirname(__FILE__);//当前脚本所在路径 require $dir."/PHPExcel_1.8.0/C ...

  5. 解决:Error: JAVA_HOME is not defined correctly

    问题重现: Error: JAVA_HOME is not defined correctly. We cannot execute :/usr/lib/jvm/java-7-oracle 问题分析: ...

  6. CentOS 6.7下利用Rsyslog+LogAnalyzer+MySQL部署日志服务器

    一.简介 LogAnalyzer 是一款syslog日志和其他网络事件数据的Web前端.它提供了对日志的简单浏览.搜索.基本分析和一些图表报告的功能.数据可以从数据库或一般的syslog文本文件中获取 ...

  7. Java面试之SpringMVC总结以及在面试中的一些问题.

    1.简单的谈一下SpringMVC的工作流程? 流程 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理 ...

  8. 手机端多种分享plugin插件地址

    //qq cordova plugin add https://github.com/iVanPan/Cordova_QQ.git --variable QQ_APP_ID=app_id 参考文档 h ...

  9. 点评前端开发工具cortex安装使用方法

    cortex安装方法: 安装最新版 sudo npm install -g cortex cortex config set registry http://registry.cortexjs.org ...

  10. Java学习资料

    微信扫码:http://v.dxsbb.com/jisuanji/Java之家:http://www.javazhijia.com/bs/biye/137.html一些 http://www.ibei ...