[Leetcode] [Python]19:remove Nth Node from End of List

Source: Internet
Author: User

#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '

19:remove Nth Node from End of List
https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/

Given A linked list, remove the nth node from the end of the 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.
Note:
Given n would always be valid.
Try to do the in one pass.

===comments by dabay===
The two-pointer-to-go solution should not conform to test instructions. A pass is required, you two hands at the same time, in fact, 2 times pass.
Idea One:
When traversing, put each node in a stack, and then more n pop to the corresponding location to delete the node. The space complexity is the length of the ListNode.
Idea two:
Use a queue of size n+1 to record the n nodes before the pointer. When the pointer is to the end, delete the second element in the queue. The complexity of space is n+1.
‘‘‘

# Definition for singly-linked list.
Class ListNode:
def __init__ (self, x):
Self.val = X
Self.next = None

Class Solution:
# @return A ListNode
def removenthfromend (self, head, N):
cursor = New_head = ListNode (0)
New_head.next = Head
Queue = []
While cursor:
Queue.append (cursor)
If Len (queue) > n + 1:
Queue.pop (0)
cursor = Cursor.next
Previous = Queue.pop (0)
To_del = Queue.pop (0)
Previous.next = To_del.next
Return New_head.next
# cursor = New_head = ListNode (0)
# New_head.next = Head
# stack = []
# while cursor:
# stack.append (cursor)
# cursor = Cursor.next
# while n > 1:
# Stack.pop ()
# n = n-1
# To_del = Stack.pop ()
# previous = Stack.pop ()
# Previous.next = To_del.next
# return New_head.next


def main ():
Sol = solution ()
root = ListNode (1)
N2 = ListNode (2)
N3 = ListNode (3)
N4 = ListNode (4)
Root.next = N2
N2.next = N3
N3.next = N4
Sol.removenthfromend (root, 1)
While Root:
Print "%s"% Root.val,
root = Root.next
Print "End"


if __name__ = = ' __main__ ':
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)

[Leetcode] [Python]19: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.