每天刷一道牛客题霸-第12天-删除链表的倒数第n个节点
题目
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @return ListNode类 */ public ListNode removeNthFromEnd (ListNode head, int n) { // write code here int count = 0; ListNode root = head; while(root!=null){ count++; root = root.next; } int index = count - n + 1; root = head; if(index != 1){ while(root != null&& index<=count){ index--; if(index == 1){ root.next = root.next.next; break; } root = root.next; } return head; }else{ return head.next; } } }#笔试题目##牛客题霸#