首页 > 其他 > 详细

19. Remove Nth Node From End of List

时间:2016-05-29 22:53:09      阅读:280      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!