How does Java traverse the last number
This article introduces the knowledge of "how to traverse the last number of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Given linked list: 1-> 2-> 3-> 4-> 5, and n = 2.After removing the second node from the end, the linked list becomes 1-> 2-> 3-> 5. Definition for singly-linked list. * public class ListNode {* int val; * ListNode next; * ListNode (int x) {val = x;} * / public class Solution {public ListNode removeNthFromEnd (ListNode head, int n) {ListNode lead = head; ListNode follow = head; for (int I = 0; I < n; iTunes +) {lead = lead.next } if (lead = = null) {head = head.next;} else {while (lead.next! = null) {lead = lead.next; follow = follow.next;} follow.next = follow.next.next;} return head That's all for the content of "how Java traverses the last number". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!