Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Definition for singly-linked list.
- # class ListNode:
- # def __init__(self, x):
- # self.val = x
- # self.next = None
- class Solution:
- def removeNthFromEnd(self, head, k):
- """
- :type head: ListNode
- :type k: int
- :rtype: ListNode
- """
- to_return, future = head, head
- for _ in range(k):
- future = future.next
- prev = None
- while future is not None:
- prev = head
- head = head.next
- future = future.next
- if prev is None:
- return head.next
- else:
- prev.next = head.next
- del head
- return to_return
Add Comment
Please, Sign In to add comment