python - What does [1] mean when we put it in the end of a function and in the end of a tuple? -


this question has answer here:

i studying other people's code, , part of code confuses me lot. [1] mean in remove(head)[1]? also, (head, head.next)[i+1 == n]? me?

in code, head head of linked list, coming class called listnode, , contains 2 functions. 1 head.val, shows value of head. 1 head.next, calls next object in linked list. here n int. code tries remove nth node end of list , return head.

for example,

given linked list: 1->2->3->4->5, , n = 2.

after removing second node end, linked list becomes 1->2->3->5.

following code:

class solution:     def removenthfromend(self, head, n):         def remove(head):             if not head:                 return 0, head             i, head.next = remove(head.next)             return i+1, (head, head.next)[i+1 == n]         return remove(head)[1] 

the function remove returns tuple(actually pair) - first value being index , second value being removed element. try solve question 1 step @ time. replace call function value returns , see if square brackets make sense.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -