/**
* 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) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
int N = 0;
ListNode p = dummy;
while (p != null) {
N ++;
p = p.next;
}
p = dummy;
for (int i = 0; i < N - n - 1; i ++) {
p = p.next;
}
p.next = p.next.next;
return dummy.next;
}
}
删除链表的倒数第 N 个结点
猜你喜欢
转载自blog.csdn.net/Lily8888888/article/details/113464012
今日推荐
周排行