In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to delete the n-th node from the bottom of the linked list in LeetCode solution". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian to study and learn "how to delete the n-th node from the bottom of the linked list in LeetCode solution" together.
Topic: Delete the n-th node from the bottom of the linked list
Give you a linked list, delete the n-th node from the bottom of the list, and return the first node of the list.
Advanced: Can you try using a scan implementation?
Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
Example 2: Input: head = [1], n = 1 Output: []
Example 3: Input: head = [1,2], n = 1 Output: [1]
Solution 1
The first easy way to think of it is to traverse the list to find the node to be deleted like an array, so solve two problems first:
1. Get the total length of the linked list
public int getLength(ListNode head){ int n=0; while(head!= null){ n++; head=head.next; } return n; }
2. After finding the node, how to delete it.
In fact, the next point across the past to delete the node on the line.
tempNode.next=tempNode.next.next;
However, the above method is to delete the tempNode.next node. If we want to delete the tempNode itself, then we must advance the first node to the first node.
For example, if we want to delete the first node of the linked list, if your own pointer points to the first node, then the deletion method above will never delete the first node.
So we need to advance the pointer to the first node.
So, in summary, we have the following solution:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { int length=getLength(head); //Create a new linked list node pointing to the head node, which is the special case to be noted above. ListNode lastNode=new ListNode(0,head); ListNode tempNode=lastNode; for(int i=0;i
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.