标签:
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.
思路:
双指针思想,先往后数n个,头和新的指针之间有n-1个节点,那么新的指针数到尾,头指针跟着往后数,指针位置的下一个节点删掉就行。
1 ListNode *t1=head; 2 ListNode *t2=head; 3 for(int i=0;i<n;i++) 4 { 5 t1=t1->next; 6 } 7 if(t1==NULL) 8 return head->next; 9 while(t1->next!=NULL) 10 { 11 t1=t1->next; 12 t2=t2->next; 13 } 14 t2->next=t2->next->next; 15 return head;
19. Remove Nth Node From End of List
标签:
原文地址:http://www.cnblogs.com/wy-chen14/p/5540548.html