当前位置:网站首页>[sword finger offer] 22 The penultimate node in the linked list
[sword finger offer] 22 The penultimate node in the linked list
2022-06-11 16:10:00 【LuZhouShiLi】
The finger of the sword Offer 22. Last in the list K node
subject

Ideas
- Initialize a front pointer former And a back pointer latter, Both pointers point to the header node head
- The front pointer moves forward first K Step ,( After that , Double pointer former and latter Distance between k Step )
- Then the two pointers move at the same time ,former and latter Take a step forward , until former Pointing empty , So now Latter The difference from the tail node k - 1 Step , That is to point to the penultimate k Nodes .
Code
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode former = head;
ListNode latter = head;
// The first pointer goes first k Step
for(int i = 0; i < k; i++)
{
if(former == null)
{
return null;
}
former = former.next;
}
// Then the first pointer differs from the second pointer k Step Because the first pointer former stay k + 1 Location latter Still in the first position
// When the first pointer jumps out former Pointing empty that latter and former Difference between k Step , That is, the difference from the tail node k - 1 Step
while(former != null)
{
// Move the first pointer to the end of the linked list
former = former.next;
latter = latter.next;
}
return latter;
}
}
边栏推荐
- What if you can't access the desktop after the computer is turned on
- Yiwenjiaohui your database system tuning
- 再聊数据中心网络
- [golang] leetcode special training - array and slice
- (湖南科技大学oj作业)问题 G: 串的模式匹配
- [Yugong series] June 2022 Net architecture class 077 distributed middleware schedulemaster loading assembly timing task
- Why are bugs changing more and more?
- TC8:UDP_ MessageFormat_ 01-02
- Go language - array
- postgresql源码编译
猜你喜欢

Opengauss database JDBC environment connection configuration (eclipse)

JDBC debugging error, ask for guidance

Classmate, have you heard of mot?

PyQt5 使QPlainTextEdit控件支持行号显示

laravel 2020-01-01T00:00:00.000000Z 日期转化

Hands on, how should selenium deal with pseudo elements?

面试经典题目:怎么做的性能测试?【杭州多测师】【杭州多测师_王sir】

Toolbar details of user interface - autorunner automated test tool

想学好ArrayList,看完这篇就够了

无心剑英汉双语诗001. 《春游》
随机推荐
收藏 | 可解释机器学习发展和常见方法!
[Yugong series] June 2022 Net architecture class 079 cluster principle of distributed middleware schedulemaster
Database resource load management (Part 2)
TC8:UDP_ MessageFormat_ 01-02
Production problem troubleshooting reference
Db4ai: database driven AI
Operation guide | how to select a collector on moonbeam and Moonriver
How AGC security rules simplify user authorization and authentication requests
Talk about data center network again
Will you be punished for not wearing seat belts in the back row?
Import data: GS_ restore or MERGE INTO? See which one suits you better
面试高频算法题---最长回文子串
搜索与图论:Dijkstra求最短路 I—Dijkstra(最短路径)
From digital twinning to digital immortality, the "three-stage theory" of the development of the meta universe
DHCP协议实例化分析
Opengauss enterprise installation
PostgreSQL create table
Yef 2022 opened yesterday. The whole process of free live broadcast on multiple network platforms opened an online technology feast!
Classmate, have you heard of mot?
Laravel8 implementation of sign in function