在没有显式修改的情况下更改python list的值的原因是什么?

时间:2020-06-15 20:40:15

标签: python

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        ListNode *ohead = new ListNode, *ehead = new ListNode;
        ListNode *optr  = ohead, *eptr = ehead;
        bool isOdd = true;
        for(auto ptr = head; ptr; ptr = ptr->next){
            if(isOdd){
                optr->next = ptr;
                optr = optr->next;
            }
            else{
                eptr->next = ptr;
                eptr = eptr->next;
            }
            isOdd = !isOdd; //update isOdd
        }
        optr->next = ehead->next;
        eptr->next = nullptr;
        return (ohead->next);
    }
};

每次迭代访问数组都会更新

def isCyclicUtil(graph,v,visit,parent): 
    visit[v]= True
    for i in graph[v]: 
        if  visit[i]==False :  
            if(isCyclicUtil(graph,i,visit,v)): 
                 return True
            elif  parent!=i: 
                 return True

    return False


def isCyclic(graph,n): 
    visited =[False]*(n) 
    for i in range(n):
        print(visited,i)
        if visited[i] == False:
            if(isCyclicUtil(graph,i,visited,-1)) == True: 
                return True


    return False



print(isCyclic({0: [1], 1: [0], 2: [3, 4], 3: [2, 4], 4: [3, 2]},5))

在上面的代码中,访问数组的值被更改,而无需对访问数组进行任何显式修改。 这是什么原因?

0 个答案:

没有答案