博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode019. Remove Nth Node From End of List
阅读量:2242 次
发布时间:2019-05-09

本文共 907 字,大约阅读时间需要 3 分钟。

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.

# 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        """        fast=head        slow=head                while n>0:            fast = fast.next            n -= 1                    if fast is None:            return head.next                while fast.next:            slow=slow.next            fast=fast.next                    slow.next = slow.next.next        slow=slow.next                return head

这道题想在O(n)时间解决,需要用到两个指针,分别设为fast和slow,中间固定距离n。

转载地址:http://ybrbb.baihongyu.com/

你可能感兴趣的文章
zookeeper客户端命令行查看dubbo服务的生产者和消费者
查看>>
intellij idea 相关搜索快捷键
查看>>
oracle查看数据库连接池中最大连接数和当前用户连接数等信息
查看>>
oracle中创建同义词(synonyms)表
查看>>
建立DB-LINK和建立视图
查看>>
普通视图和物化视图的区别(转)
查看>>
物化视图加DBLINK实现数据的同步_20170216
查看>>
Redis在京东到家的订单中的使用
查看>>
idea 注释模板设置
查看>>
单例模式singleton为什么要加volatile
查看>>
Oracle_spatial的空间操作符
查看>>
SDO_GEOMETRY结构说明
查看>>
oracle 的 SDO_GEOMETRY
查看>>
往oracle中插入geometry的两种方式
查看>>
Oracle Spatial中的Operator操作子 详细说明
查看>>
Oracle Spatial中SDO_Geometry详细说明
查看>>
oracle 聚合函数 LISTAGG ,将多行结果合并成一行
查看>>
Oracle列转行函数 Listagg() 语法详解及应用实例
查看>>
LISTAGG函数的用法
查看>>
Oracle Spatial操作geometry方法
查看>>