当前位置:网站首页>Sword finger offer 22 The penultimate node in the linked list - speed pointer
Sword finger offer 22 The penultimate node in the linked list - speed pointer
2022-04-23 17:33:00 【hequnwang10】
One 、 Title Description
Enter a linked list , Output the last number in the list k Nodes . In order to conform to the habits of most people , From 1 Start counting , That is, the tail node of the list is the last 1 Nodes .
for example , A list has 6 Nodes , Start from the beginning , Their values, in turn, are 1、2、3、4、5、6. The last of the list 3 Each node has a value of 4 The node of .
Example 1:
Given a linked list : 1->2->3->4->5, and k = 2.
Back to the list 4->5.
Two 、 Problem solving
Speed pointer
There is a difference between the speed pointer and the slow pointer K Nodes , Then update the speed pointer
/** * 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) {
// Speed pointer
if(head == null){
return head;
}
ListNode fast = head;
ListNode slow = head;
for(int i = 0;i<k;i++){
fast = fast.next;
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009222.html
边栏推荐
- 41. 缺失的第一个正数
- Future 用法详解
- Shell-sort命令的使用
- stm32入门开发板选野火还是正点原子呢?
- ECMAScript history
- Using quartz under. Net core -- a simple trigger of [7] operation and trigger
- Baidu Map 3D rotation and tilt angle adjustment
- Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
- Use of Shell sort command
- 为什么有些人说单片机简单,我学起来这么吃力?
猜你喜欢

Exercise: even sum, threshold segmentation and difference (two basic questions of list object)

Understanding of RPC core concepts

XTask与Kotlin Coroutine的使用对比

2021长城杯WP

394. 字符串解码-辅助栈

HCIP第五次实验

C# Task. Delay and thread The difference between sleep
![Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers](/img/65/89473397da4217201eeee85aef3c10.png)
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers

Future 用法详解

In embedded system, must the program code in flash be moved to ram to run?
随机推荐
Shell - introduction, variables, and basic syntax
31. 下一个排列
Collection of common SQL statements
双闭环直流调速系统matlab/simulink仿真
Conversion between hexadecimal numbers
In ancient Egypt and Greece, what base system was used in mathematics
Further optimize Baidu map data visualization
Devexpress GridView add select all columns
Indexes and views in MySQL
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
Learning record of uni app dark horse yougou project (Part 2)
圆环回原点问题-字节跳动高频题
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
[related to zhengheyuan cutting tools]
HCIP第五次实验
[WPF binding 3] listview basic binding and data template binding
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
Advantages and disadvantages of several note taking software
Abnormal resolution of Xiaomi camera