Remove Nth Node From End of List
Remove Nth Node From End of List
Example
Solution
if head == None:
return None
head = head.next
from head move it to n steps/Last updated
if head == None:
return None
head = head.next
from head move it to n steps/Last updated
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of linked list.
@param n: An integer.
@return: The head of linked list.
"""
def removeNthFromEnd(self, head, n):
# write your code here
if n <= 0:
return head
dummy = ListNode(0)
dummy.next = head
preDelete = dummy
for i in range(n):
if head == None:
return None
head = head.next
while head:
head = head.next
preDelete = preDelete.next
preDelete.next = preDelete.next.next
return dummy.next