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

Start the Journey
N2I -2020.04.02

1. My first solution

  1. class Solution:
  2. def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
  3. con=0
  4. p=head
  5. while p!=None:
  6. p=p.next
  7. con+=1
  8. con-=n
  9. #print(head,con)
  10. p=head
  11. #print(p)
  12. if con==0:
  13. head=head.next
  14. return head
  15. for i in range(con-1):
  16. p=p.next
  17. p.next=p.next.next
  18. return head

Explanation:

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