lintcode|lintcode 174. 删除链表中倒数第n个节点
【lintcode|lintcode 174. 删除链表中倒数第n个节点】难度:容易
1. Description
文章图片
174. 删除链表中倒数第n个节点 2. Solution
- python
pre_n指向的位置在cur指向位置的前面第n个。
当cur是链表尾部时,pre_n正好是倒数第n个。
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""class Solution:
"""
@param head: The first node of linked list.
@param n: An integer
@return: The head of linked list.
"""
def removeNthFromEnd(self, head, n):
# write your code here
if n==1:
if head.next is None:
return None
pre_n = head
cur = head
for i in range(n):
cur = cur.next
while cur.next is not None:
pre_n = pre_n.next
cur = cur.next
pre_n.next = pre_n.next.next
return head
pre_n = head
cur = head
for i in range(n-1):
cur = cur.next
while cur.next is not None:
pre_n = pre_n.next
cur = cur.next
pre_n.val = pre_n.next.val
pre_n.next = pre_n.next.next
return head
3. Reference
- https://www.lintcode.com/problem/remove-nth-node-from-end-of-list/description?_from=ladder
推荐阅读
- LintCode|LintCode 545 [Top k Largest Number II]
- win7删除新建不自动刷新
- bu删除,不拉黑,不联系
- python|8. 文件系统——文件的删除、移动、复制过程以及链接文件
- vue|vue 上移 下移 删除 排序
- xxxxxxx超市水果采购管理问题研究(若涉及侵权,请及时联系,立马删除)
- Github上文件传输/修改/删除的新手教程
- 用vue以对象的形式添加删除元素和购物车
- docker|docker image container 删除
- 2020-05-24怎样删除表格里的文字只保留数字