当前位置:网站首页>[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
边栏推荐
- [leetcode refers to the substructure of offer 26. Tree (medium)]
- Leetcode-279-complete square number
- wait、waitpid
- Flomo software recommendation
- Automatic heap dump using MBean
- Centralized record of experimental problems
- 1.整理华子面经--1
- Getting started with detectron2
- [SDU chart team - core] enumeration of SVG attribute class design
猜你喜欢
[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
MySQL数据库常识之储存引擎
opencv应用——以图拼图
Deep analysis of C language pointer (Part I)
Use 3080ti to run tensorflow GPU = 1 X version of the source code
41. The first missing positive number
常用60类图表使用场景、制作工具推荐
MySQL基础之写表(创建表)
Flomo software recommendation
GSI-ECM工程建设管理数字化平台
随机推荐
Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
presto on spark 支持3.1.3记录
mmap、munmap
Alibaba cloud responded to the disclosure of user registration information
Thread safe sigleton (singleton mode)
Reference of custom message in ROS function pack failed
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
Win 11K in 100 days, super complete learning guide for job transfer test
MySQL基础合集
unity 功能扩展
How to learn software testing? Self study or training? After reading this article, you will understand
Automatic heap dump using MBean
41. The first missing positive number
On the three paradigms of database design
Express③(使用Express编写接口、跨域有关问题)
[leetcode refers to the substructure of offer 26. Tree (medium)]
Unit function expansion
Awk example skills
Crisis is opportunity. Why will the efficiency of telecommuting improve?
使用mbean 自动执行heap dump