4.12
还是要注意边界值的处理啊
总不是能一次考虑好
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The first node of linked list.
* @param n: An integer.
* @return: The head of linked list.
*/
ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null){
return head;}
ListNode tmp = head;
ListNode flag = head;
for(int i = 0;i < n;i++){
if(tmp.next != null){
tmp = tmp.next;
}
else{
break;//return head;
}
}
while(tmp.next !=null){
tmp = tmp.next;
flag = flag.next;
}
if(flag == head){
return head.next;
}
flag.next = flag.next.next;
return head;// write your code here
}
}