当前位置:网站首页>[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
边栏推荐
- airbase 初步分析
- laravel 发送邮件
- go reflect
- [leetcode refers to offer 10 - I. Fibonacci sequence (simple)]
- C, print the source program of beautiful bell triangle
- PHP的Laravel与Composer部署项目时常见问题
- [※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
- Pikachuxss how to get cookie shooting range, always fail to return to the home page
- Detectron2 usage model
- Deep analysis of C language function
猜你喜欢

Recommended usage scenarios and production tools for common 60 types of charts

wait、waitpid

Opencv application -- jigsaw puzzle

DeNO 1.13.2 release

管道和xargs


Prim、Kruskal
![[leetcode refers to the maximum profit of offer 63. Stock (medium)]](/img/37/478b53696f9327b7d435cdd887dd57.png)
[leetcode refers to the maximum profit of offer 63. Stock (medium)]
![[leetcode refers to offer 10 - I. Fibonacci sequence (simple)]](/img/f9/22a379f330c3ee21a2a386bbd4a98f.png)
[leetcode refers to offer 10 - I. Fibonacci sequence (simple)]

Problem brushing plan -- dynamic programming (IV)
随机推荐
MySQL advanced common functions
Recommended usage scenarios and production tools for common 60 types of charts
Keras. Layers introduction to various layers
unity 功能扩展
Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
[※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
Common commands of MySQL in Linux
Use 3080ti to run tensorflow GPU = 1 X version of the source code
3-5 obtaining cookies through XSS and the use of XSS background management system
MySQL进阶之数据的增删改查(DML)
setInterval、setTimeout、requestAnimationFrame
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
setInterval、setTimeout、requestAnimationFrame
DeNO 1.13.2 release
[leetcode refers to offer 27. Image of binary tree (simple)]
Yolov5 NMS source code understanding
Centralized record of experimental problems
pytorch 1.7. The model saved by X training cannot be loaded in version 1.4 or earlier
常用60类图表使用场景、制作工具推荐