Guest User

Untitled

a guest
Dec 14th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6.  
  7. class Solution:
  8. def removeNthFromEnd(self, head, k):
  9. """
  10. :type head: ListNode
  11. :type k: int
  12. :rtype: ListNode
  13. """
  14. to_return, future = head, head
  15. for _ in range(k):
  16. future = future.next
  17.  
  18. prev = None
  19. while future is not None:
  20. prev = head
  21. head = head.next
  22. future = future.next
  23.  
  24. if prev is None:
  25. return head.next
  26. else:
  27. prev.next = head.next
  28. del head
  29. return to_return
Add Comment
Please, Sign In to add comment