Untitled

 avatar
unknown
plain_text
5 months ago
985 B
2
Indexable
# Definition for singly-linked list.
class ListNode:
	def __init__(self, x):
		self.val = x
		self.next = None

class Solution:
	# @param A : head node of linked list
	# @param B : integer
	# @return the head node in the linked list
	def removeNthFromEnd(self, A, B):

            temp = A

            count =0
            temp = A
            while temp is not None:
                temp = temp.next
                count +=1     

            if count -B ==0:
                return A.next

            if B==1:
                temp = A
                while temp.next is not None:
                    temp.next = None
                return A

            if count<B:
                return A.next

            temp = A
            position = 0
            while temp is not None and position< count-B:
                position+=1
                temp = temp.next
            temp.next = temp.next.next

            return A


            
Editor is loading...
Leave a Comment