Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of the last k node in the linked list by LeetCode

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces LeetCode how to solve the problem of the penultimate k node in the linked list, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Title

Enter a linked list and output the last but one node in the linked list. In order to meet the habits of most people, this topic starts counting from 1, that is, the end node of the linked list is the penultimate node. For example, a linked list has six nodes, starting from the top node, and their values are 1, 2, 3, 4, 5, and 6. The penultimate node of this linked list is the node with a value of 4.

Example: given a linked list: 1-> 2-> 3-> 4-> 5, and k = 2. Return to linked list 4-> 5. Train of thought

Initialization: the front pointer former, the rear pointer latter, and both pointers point to the head node head.

Build the double pointer distance: the front pointer former takes k steps forward first (after the end, the distance between the double pointer former and latter is k steps).

Double pointers move together: in the loop, the double pointers former and latter take one step forward each round until former jumps out as it passes the tail node of the linked list (after the jump, the distance between the latter and the tail node is KMY 1, that is, latter points to the last-to-last node).

Return value: just return latter.

Code / * 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) {ListNode former = head, latter = head; for (int I = 0; I < k; iBo +) {former = former.next;} while (former! = null) {former = former.next Latter = latter.next;} return latter;}} Thank you for reading this article carefully. I hope the article "how to solve the problem of the last k nodes in the linked list" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report