[LeetCode][python3]0019. Remove Nth Node From End of List

Start the Journey
N2I -2020.04.02

1. My first solution

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        con=0
        p=head
        while p!=None:
            p=p.next
            con+=1
        con-=n
        #print(head,con)
        p=head
        #print(p)
        if con==0:
            head=head.next
            return head
        
        for i in range(con-1):
            p=p.next
        p.next=p.next.next
        return head

Explanation:

The Solution count the total in the first for loop, then delete the element in the second one.


Comments