力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现

力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现,第1张

概述题目描述: 中文: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 1->2->3->5. 说明: 给定的 n 保证是有效的。 进阶: 你能尝试使用一趟扫描实现吗? 英文: Given a linked list, remove the n-th node from th

题目描述:

中文:

给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。

示例:

给定一个链表: 1->2->3->4->5,和 n = 2.

当删除了倒数第二个节点后,链表变为 1->2->3->5.

说明:

给定的 n 保证是有效的。

进阶:

你能尝试使用一趟扫描实现吗?

英文:

Given a linked List,remove the n-th node from the end of List and return its head.

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.

Follow up:

Could you do this in one pass?

# DeFinition for singly-linked List.# class ListNode(object):#     def __init__(self,x):#         self.val = x#         self.next = Noneclass Solution(object):    def removeNthFromEnd(self,head,n):        """        :type head: ListNode        :type n: int        :rtype: ListNode        """        dummy = ListNode(0)        dummy.next = head        p1=p2=dummy        for i in range(n):            p1 = p1.next        while p1.next:            p1=p1.next            p2=p2.next        p2.next = p2.next.next        return dummy.next        

 

题目来源:力扣

总结

以上是内存溢出为你收集整理的力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现全部内容,希望文章能够帮你解决力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1190664.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存