Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

面试题 02.02. 返回倒数第 k 个节点 #66

Open
Labels
@Geekhyt

Description

@Geekhyt

原题链接

双指针

  1. 在头节点分别定义快、慢两个指针,在定义 n 计数器
  2. 快指针先行,直到与慢指针相差 k 时,慢指针也开始走
  3. 这样的话,当快指针遍历完成时,慢指针就刚好在倒数第 k 个值的位置了
constkthToLast=function(head,k){letfast=headletlow=headletn=0while(fast){fast=fast.nextif(n>=k){low=low.next}n++}returnlow.val}
  • 时间复杂度 O(n)
  • 空间复杂度 O(1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp