In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to solve the problem of finding the last-to-last node of the leetcode linked list, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
Enter a linked list and output the last-to-last 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. Source: LeetCode link: https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. Answer the question / 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 C1 = head; while (C1! = null & & k > 0) {C1 = c1.nextKmurf;} ListNode c2 = head While (C1! = null) {C1 = c1.next; c2 = c2.next;} return c2;}}
Fast and slow pointer, first let the fast pointer take k steps, and then the two pointers go synchronously. When the fast pointer reaches the end, the slow pointer is the last-to-last node of the linked list.
Thank you for reading this article carefully. I hope the article "how to solve the problem of finding the last-to-last node of the leetcode linked list" shared by the editor will be helpful to everyone. At the same time, I hope you will support us 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.