In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 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.
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.