Remove Nth Node from End of List and linked list Topics summary

Source: Internet
Author: User

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

For example,

   n = 2.   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n would always be valid.
Try to do the in one pass.

Linked list of a simple topic, the topic requires only one pass to sweep the linked list, but generally do not know the total length of the list, you can not know the bottom of the nth node (the actual consideration is the penultimate N+1 node) where. Use is a common technique in the list, maintaining a previous two pointers fast and slow. The fast pointer moves n steps ahead of the slow pointer. So when the fast pointer points to the last element of the list, the slow pointer points to the reciprocal (n+1) node and then handles the deletion of the element.

It is important to note that if you delete a head node , after fast takes n steps (equal to the list length), Fast is none, and then when you move fast and slow at the same time, judging fast.next to determine whether the end node is reached will directly result in an access error. There are two ways to solve this: the first is to determine if fast is already none, or none, when fast first walks n steps, which means that the head node is deleted, and the Head.next is returned. The second is to add a dummy dummy element, fast after the N-step, even if the deletion of the head element, it will also point to the tail node. Prevent post-judgment errors, but this approach has a disadvantage, that is, more auxiliary elements, and the actual fast and slow have gone 1 more steps.

Method one code:
classsolution (object):defremoventhfromend (self, head, N):""": Type Head:ListNode:type n:int:rtype:listnode"""Fast= Slow =Head for_inchrange (N): Fast=Fast.nextif  notFast:returnHead.next whileFast.next:fast=Fast.next Slow=Slow.next Slow.next=Slow.next.nextreturnHead

Method two code:

classsolution (object):defremoventhfromend (self, head, N):""": Type Head:ListNode:type n:int:rtype:listnode"""        if  notHead:returnNone Dummy= ListNode (-1) Dummy.next=Head Fast= Slow =Dummy forIinchrange (N): Fast=Fast.next whileFast.next:slow=Slow.next Fast=Fast.next Slow.next=Slow.next.nextreturnDummy.next

Note that the topic itself says that N is legal, but in the actual interview this reasonable assumption usually does not exist, so need to add some reasonable judgment, judge whether it is legal, here in advance assume that the cross-border directly back to head. Based on the first solution, change the following:

classsolution (object):defremoventhfromend (self, head, N):""": Type Head:ListNode:type n:int:rtype:listnode"""Fast= Slow =head I=0 whileI < n andFast:fast=Fast.next i+ = 1ifI < n-1:            returnHeadif  notFast:returnHead.next whileFast.next:fast=Fast.next Slow=Slow.next Slow.next=Slow.next.nextreturnHead
in summary: The list of points to note is: 1. Out of bounds: prone to memory access errors, such as calling None.next. Especially for the special case of the empty list. 2. Update head for special handling 3. A pointer to the next moving position is not retained when the node is deleted (more for reverse linked list). 4. There is a +-1 deviation in the moving position.
Common techniques:
1. Dummy Head: Simplify the change and remove the handle of the head pointer.
2. Front and rear double pointers: more for list reversal.

Remove Nth Node from End of List and linked list Topics summary

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.