当前位置:网站首页>Sword finger offer 22 The penultimate node in the linked list
Sword finger offer 22 The penultimate node in the linked list
2022-06-30 05:31:00 【Grayson Zhang】
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 :
Given a linked list : 1->2->3->4->5, and k = 2.
Back to the list 4->5.
Answer key
Traverse twice , The first pass counts the total length of the linked list , Find the positive position of the reciprocal node
Second times , Find the node
1. python
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
cur = head
count = 0
while cur:
count += 1
cur = cur.next
cur = head
count -= k
while count:
cur = cur.next
count -= 1
return cur

2. C Language
struct ListNode* getKthFromEnd(struct ListNode* head, int k){
struct ListNode *cur = head;
int count = 0;
while (cur != NULL) {
count += 1;
cur = cur -> next;
}
cur = head;
count -= k;
while (count != 0) {
count -= 1;
cur = cur -> next;
}
return cur;
}

3.C++
and C equally , Just change a variable declaration
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
ListNode *cur = head;
int count = 0;
while (cur != NULL) {
count += 1;
cur = cur -> next;
}
cur = head;
count -= k;
while (count != 0) {
count -= 1;
cur = cur -> next;
}
return cur;
}
};

边栏推荐
- Revit secondary development - use panel function without opening the project
- Unity project hosting platform plasticscm (learn to use 1)
- Digital signature——
- Solidity - 安全 - 重入攻击(Reentrancy)
- token 过期后,如何自动续期?
- 强烈推荐十几款IDEA开发必备的插件
- 炒股用指南针开户交易安全吗?
- mmdet之Loss模块详解
- Delete the repeating elements in the sorting list (simple questions)
- 3D rotation album
猜你喜欢

Bev instance prediction based on monocular camera (iccv 2021)

Unity shader flat shadow

3D rotation album

OpenCL线程代数库ViennaCL的使用

Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation

Unity project hosting platform plasticscm (learn to use 1)

2022年,谁在推动音视频产业的新拐点?

Virtual and pure virtual destructions

虚析构和纯虚析构

Unity publishing /build settings
随机推荐
Bessel curve with n control points
UML tools
使用码云PublicHoliday项目判断某天是否为工作日
How to prevent source code leakage in enterprises and institutions
Unity shortcut key
Qt之QListView的简单使用(含源码+注释)
Database SQL language 05 SQL exercise
RedisTemplate 常用方法汇总
Responsive flow layout
东塔攻防世界—xss绕过安全狗
Online assignment of C language program design in the 22nd spring of Western Polytechnic University
Virtual and pure virtual destructions
GoLand No Tests Were Run : 不能使用 fmt.Printf() <BUG>
C. Divan and bitwise operations
Detailed explanation of the loss module of mmdet
Rotation, translation and scaling of unity VR objects
Why can transformer break into the CV world and kill CNN?
Display steerable 3D model in front of unity UI
抓取手机端变体组合思路设想
Pytorch的安装以及入门使用