/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode fakeHead = new ListNode(-1);
fakeHead.next = head;
ListNode run = fakeHead;
ListNode preRun = fakeHead;
for (int i=0; i<n; i++){
preRun = preRun.next;
}
while(preRun.next != null){
run = run.next;
preRun = preRun.next;
}
run.next = run.next.next;
return fakeHead.next;
}
}
Remove Nth Node From End of List
最新推荐文章于 2021-08-04 10:48:57 发布