19. Merge Intervals
[Medium] “Given a linked list, remove the n-th node from the end of list and return its head."
Python3:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
if (not head) or (not head.next):
return None
pointer_left = head
pointer_right = head
# shift pointer right by n
for i in range(0, n):
pointer_right = pointer_right.next
# short case
if not pointer_right:
return head.next
while pointer_right.next:
pointer_left = pointer_left.next
pointer_right = pointer_right.next
# remove nth element
pointer_left.next = pointer_left.next.next
return head