题解 |
删除链表的倒数第n个节点
http://www.nowcoder.com/practice/f95dcdafbde44b22a6d741baf71653f6
```import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode fast = new ListNode(0);
ListNode slowRe = new ListNode(0);
slowRe.next = head;
ListNode slow = slowRe;
fast.next = head;
ListNode pre = slow;
for (int i = 0; i < n && fast!=null; i++) {
fast = fast.next;
}
while (fast!=null) {
fast = fast.next;
pre = slow;
slow = slow.next;
}
pre.next = slow.next;
return slowRe.next;
}
}