Remove Nth Node from End of List

Source: Internet
Author: User

Given A linked list, remove the nth node from the end of the list and return its head.

For example,

   1->2->3->4->5  N = 2.   1->2->3->5.
Ideas:
Double pointer thought, first number of N, the head and the new pointer between the N-1 node, then the new pointer number to the tail, the head pointer followed by the number of pointers, the pointer position of the next node deleted on the line.
1ListNode *t1=head;2ListNode *t2=head;3  for(intI=0; i<n;i++)4 {5T1=t1->Next;6 }7 if(t1==NULL)8     returnHead->Next;9  while(t1->next!=NULL)Ten { OneT1=t1->Next; AT2=t2->Next;  - } -T2->next=t2->next->Next; the returnHead

Remove Nth Node from End of List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.