We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
https://leetcode.com/problems/remove-nth-node-from-end-of-list/tabs/description
Given a linked list, remove the nth node from the end of 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 will always be valid. Try to do this in one pass.
双指针:
class ListNode(object): def __init__(self, x): self.val = x self.next = None def __str__(self): return "Node({0}->[{1}])".format(self.val, self.next) def init_list(l): before = None for i in l[::-1]: n = ListNode(i) n.next = before before = n return n class Solution(object): def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ root = ListNode(None) root.next = head first = root second = root for i in range(n): first = first.next while first.next != None: first = first.next second = second.next second.next = second.next.next return root.next print Solution().removeNthFromEnd(init_list([1,2,3,4,5]), 2)
The text was updated successfully, but these errors were encountered:
#4 20170728
Sorry, something went wrong.
No branches or pull requests
问题
https://leetcode.com/problems/remove-nth-node-from-end-of-list/tabs/description
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Note:
Given n will always be valid.
Try to do this in one pass.
思路
双指针:
解答
The text was updated successfully, but these errors were encountered: