[Leetcode] #19 Remove Nth Node from the End of list

Source: Internet
Author: User

From the move back one months, has been busy in the project application of things. By writing an application, the press release of the news is beginning to look pleasing to the eye.

Brother elder sister has already started to look for a job, I have one year, work early preparation. Machine learning continues to derive common algorithms, brush a brush kaggle, do feature engineering what, is applied.

Today this problem is not difficult, but I wrote under Linux, some local debugging methods and techniques to record.

First, the topic

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.

Second, the analysis

A linked list, removing the nth node from the bottom, preferably using a traversal method.

The first reaction, of course, is to iterate through the list and see how long it is, and then iterate through the list and delete the nth of the count.

After seeing a traversal, we decide to use space for time to open a list of the address of the node, so that only two or three steps in the list to achieve the deletion of nodes

Third, the Code

1 #Definition for singly-linked list.2 #class ListNode (object):3 #def __init__ (self, x):4 #self.val = x5 #Self.next = None6 7 classsolution (object):8     defremoventhfromend (self, head, N):9         """Ten : Type Head:listnode One : Type N:int A : Rtype:listnode -         """ -Address = [] the          whileHead: - Address.append (head) -Head =Head.next -          +Length =Len (address) -         ifLength = =0: +             returnAddress[0] A         eliflength = = 1: at             ifn = =0: -                 returnAddress[0] -             elifn = = 1: -                 return [] -         Else: -             ifn = =0: in                 returnAddress[0] -             elifLength = =N: to                 returnAddress[1] +             Else: -target = Address[length-N] thep =Target.next *Address[length-n-1].next =P $                 del(Address[length-N])Panax Notoginseng                 returnAddress[0]

Iv. Harvesting

1. The first two days of the installation of CentOS, with vim to write this program. In the local debugging, encountered a problem, usually did not write a class, suddenly will not debug. After the guidance of master, through a little bit of knowledge of the reserve, and finally the fix.

The program structure should look like this:

1 ########## #q19. py###############2 Class ListNode (object):3     def __init__(self, x):4Self.val =x5Self.next =None6 7 Class Solution (object):8     defremoventhfromend (self, head, N):9         ....Ten         returnHead One      A     defMain (self): -         #different classes can be directly used to declare objects -Head = linklist (2) the  -         #in the class, call the same method with Self.function () -linklist = Self.removenthfromend (Head, 3)   -  +         returnlinklist -      +SL =solution () A Sl.main () at  - ############# #终端调试 ############ - Importq19 -SL =q19. Solution () -Linklist =Q19.main () -  in  -In fact, it is not necessary to solution in the Def Main (), the direct sl.removenthfromend can also call the function, but in main is still more convenient.

[Leetcode] #19 Remove Nth Node from the 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.