[Leetcode] [019] Remove Nth Node from End of List (Java)

Source: Internet
Author: User

The title is here: https://leetcode.com/problems/remove-nth-node-from-end-of-list/

"Label" Linked List; Pointers

"Personal Analysis"

This topic should be considered as the basic problem in linked list. It is not based on its simplicity, but because it is the cornerstone. Some of the classic methods in the Linked list are used in this problem.

1. The pointers in the Linked list: If you can pre-link the list to know the length, then the topic is much simpler, delete the beginning of the Len-n is finished. However, there is no way for linkedlist to know the length of the list in advance, so it is necessary to use offset to solve the problem with double pointers.

1.1) set two pointers, fast and slow.

1.2) Let fast Walk Alone n steps (so that fast and slow have a fixed gap: N).

1.3) Then two pointers go forward together, and when fast.next = null, that is, fast is the last node in the list, slow refers to the node at the bottom of the nth + 1 node, which is the one we want to delete in front of the node.

1.4) Let Slow.next = Slow.next.next, so that the successful deletion of the original Slow.next

2. Dummy head application: Dummy Head is we create a new node, and then let the original head node to the back of the new node. I think using the dummy head with three more obvious benefits.

One is to minimize the changes to the original linked list, if the head = Head.next Such a method, head will move, so change the original situation, not very good.

Two benefits are convenient to return, directly return to Dummy.next can be.

Three is not the right to do special treatment, if the deletion of the node is exactly the head node, but also for this situation to write some code, increase the amount of code.

In short, a new dummy head is probably a better choice if it is required to return a list of new head nodes.

1  Public classSolution {2      PublicListNode Removenthfromend (ListNode head,intN) {3         if(Head = =NULL|| N <= 0) {4             return NULL;5         }6ListNode Dummyhead =NewListNode (-1);7Dummyhead.next =head;8         9ListNode fast =Dummyhead;TenListNode slow =Dummyhead; One          A         //Let fast point go n steps itself -          for(inti = 0; I < n; i++) { -             assert(Fast! =NULL); theFast =Fast.next; -         } -          -         //fast and slow go together +         //until fast reaches the end of list -          while(Fast.next! =NULL) { +Fast =Fast.next; Aslow =Slow.next; at         } -          -         //Now slow should pointing to the node -         //before which we want to remove -Slow.next =Slow.next.next; -  in         returnDummyhead.next; -     } to}

[Leetcode] [019] Remove Nth Node from End of List (Java)

Related Article

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.