Given a linked list, remove the nth node from the end of list and return its head.
For example, Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
• Given n will always be valid.
• Try to do this in one pass.
这道题就不说了,思路很简单,让一个指针滞后K位即可
public static void solution_2_2_7(Node head,int k){
Node p1=head,p2=head;
for(int i=1;i<=k;i++){
p2=p2.next;
}
while(p2.next!=null){
p2=p2.next;
p1=p1.next;
}
p1.next=p1.next.next;
for(Node r=head;r!=null;r=r.next){
System.out.print(r.data+" ");
}
}