当前位置:网站首页>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;
}
};

边栏推荐
- Unity project hosting platform plasticscm (learn to use 2)
- Unity 3D model operation and UI conflict Scrollview
- Qt之QListView的简单使用(含源码+注释)
- Introduction to mmcv common APIs
- Revit Secondary Development - - Project use Panel features not opened
- 3D rotation album
- Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
- Expansion method of unity scanning circle
- 旋转框目标检测mmrotate v0.3.1入门
- mmcv常用API介绍
猜你喜欢

强烈推荐十几款IDEA开发必备的插件

Installation and getting started with pytoch

14x1.5cm竖向标签有点难,VFP调用BarTender来打印

14x1.5cm vertical label is a little difficult, VFP calls bartender to print

Redistemplate common method summary

企事业单位源代码防泄露工作该如何进行

Solidity - 安全 - 重入攻击(Reentrancy)

Solitidy - fallback 回退函数 - 2种触发执行方式
![[typescript] cannot redeclare block range variables](/img/52/2fd3071ca9e3c5023c6b65961e2cf7.jpg)
[typescript] cannot redeclare block range variables

Virtual and pure virtual destructions
随机推荐
聲網,站在物聯網的“土壤”裏
Database SQL language 03 sorting and paging
Set a plane to camera viewport
VFPBS上传EXCEL并保存MSSQL到数据库中
Xijiao 21 autumn "motor and drive" online homework answer sheet (III) [standard answer]
Revit secondary development - use panel function without opening the project
Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
Unity project hosting platform plasticscm (learn to use 1)
Introduction to Redux: initial experience of Redux
[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
Promise知识点拾遗
Learning about functions QAQ
PKCs 12:personal information exchange syntax v1.1 translation part I
终端便捷ssh(免密)连接
[Motrix] download Baidu cloud files using Motrix
Unit asynchronous jump progress
抓取手机端变体组合思路设想
Rotating frame target detection mmrotate v0.3.1 learning configuration
Visualization of 3D geological model based on borehole data by map flapping software
86. 分隔链表