当前位置:网站首页>[leetcode refers to offer 22. The penultimate node in the linked list (simple)]
[leetcode refers to offer 22. The penultimate node in the linked list (simple)]
2022-04-23 21:21:00 【Minaldo7】
subject :
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 :
Given a linked list : 1->2->3->4->5, and k = 2.
Back to the list 4->5.
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process ①:
Double 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) {
if(head==null)
return null;
ListNode first=head,second=head;
int len=0;
while(first.next!=null){
len++;
first = first.next;
}
while(len-k+1>0){
second = second.next;
len--;
}
return second;
}
}
Execution results ①:

The problem solving process ②:
Single 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) {
if(head==null)
return null;
ListNode cur=head;
while(cur!=null){
cur = cur.next;
if(k==0)
head = head.next;
else
k--;
}
return head;
}
}
Execution results ②:

版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479252.html
边栏推荐
- Deno 1.13.2 发布
- YOLOv5 Unable to find a valid cuDNN algorithm to run convolution
- wait、waitpid
- FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
- presto on spark 支持3.1.3记录
- [leetcode refers to offer 27. Image of binary tree (simple)]
- Awk example skills
- Centralized record of experimental problems
- IOT design and development
- Some grounded words
猜你喜欢
随机推荐
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
又一款数据分析神器:Polars 真的很强大
MySQL进阶之常用函数
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
Detailed explanation of basic assembly instructions of x86 architecture
Problem brushing plan -- dynamic programming (IV)
Leetcode-279-complete square number
【SDU Chart Team - Core】SVG属性类设计之枚举
浅谈数据库设计之三大范式
unity 功能扩展
Norm normalization in tensorflow and pytorch of records
Common commands of MySQL in Linux
引入结构化并发,Swift 5.5 发布!
ubutnu20安装CenterNet
Introduce structured concurrency and release swift 5.5!
管道和xargs
Question brushing plan - depth first search (II)
Problem brushing plan -- dynamic programming (III)
Zhongchuang storage | how to choose a useful distributed storage cloud disk
Arm architecture assembly instructions, registers and some problems









