Question Description My Key
package LeetCode;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public class Test {
public static void main(String[] args) {
}
//refer: https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/solution/shan-chu-lian-biao-de-dao-shu-di-nge-jie-dian-by-l
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
int length = 0;
ListNode first = head;
while (first != null) {
length++;
first = first.next;
}
length -= n;
first = dummy;
while (length > 0) {
length--;
first = first.next;
}
first.next = first.next.next;
return dummy.next;
}
}
refer: https://leetcode-cn.com/problems/valid-parentheses/solution/you-xiao-gua-hao-shi-yong-arraylistzuo-wei-fu-zhu-