2013-03-01 1 views
0

글쎄, 나는 다음을 수행했는데 원래 목록의 끝에 역방향 연결 목록을 추가합니다. 따라서 lista = No (1, No (4, No (2, None))) (1> 4> 2) 인 함수 (list)를 수행하면 반환됩니다 (1> 2> 4> 4> 2> 1).문자 변경 및 연결 목록 역전. <pre><code>class No: def __init__(self, valor, prox): self.valor = valor self.prox = prox </code></pre> <p></p>가 전화했을 때 2 층과 3을 교환 할 것이라고 함수를 만들기 위해 ...</p> <p>(고전 건축) 클래스 노드를 사용 : (파이썬)는

문제는 그 문제를 일반 목록에 추가하고 거기에 얽혀 문제를 해결한다는 것입니다. 그러나 그때 나는 내가 (노드 클래스는 내가 위에서 넣어) 만 사용 체인 목록에했는데 발견 지금은 우둔 조금 ... 잘못된 솔루션

코드 해요 :

class No: 
def __init__(self, valor, prox): 
    self.valor = valor 
    self.prox = prox 


def printLista(lista): 
    global lista1 
    lista1 = [] 
    while lista: 
     lista1.append(lista.valor) 
     lista = lista.prox 
    return lista1 


def printbackwards(lista): 
    global lista2 
    if lista == None: return 
    printbackwards(lista.prox) 
    lista2.append(lista.valor) 


def swapprint(lista): 
    global lista1, lista2 
    i = 0 
    lista2 = [] 
    printlist(lista) 
    printbackwards(lista) 
    for i in range(len(lista1)): 
     print lista1[i], lista2[i], 



lista = No(3, No(1, No(4, No(2, None)))) 
swapprint(lista) 

답변

0
class No: 
    def __init__(self,a,b): 
     self.val = a 
     self.next = b 
    def __str__(self): 
     return "%s->%s"%(self.val,self.next) 

def swapandReverse(lista): 
    n2 = lista.next #2nd element 
    n2.val,n2.next.val = n2.next.val,n2.val #swap 2,3 
    n = lista #root node 
    v = [] #hold our values 
    while n.next: 
     v.append(n.val) #add our value to list 
     n = n.next #move to next node 
    v.append(n.val) #append value of last node in the list 
    while len(v): #as long as we have values left in list 
     n.next = No(v.pop(-1),None) #set next to new node with our val 
     n = n.next 


lista = No(3,No(1,No(4,No(2,None)))) 
print lista 
swapandReverse(lista) 
print lista 

적어도 이와 비슷한 것

0

연결된 목록 작업에 전역 변수를 사용할 필요가 없습니다. 대신, 올바른 방법으로 재귀 호출하고 호출 스택 위로 모든 값을 반환해야합니다. print 함수가 실제로 아무 것도 인쇄하지 않기 때문에 내가 무엇을해야하는지 이해할 수 있을지 확신하지 못합니다. 그러나 이전 목록에서 새 목록을 만들면 어떻게 할 수 있습니까?

class Node(object): 
    def __init__(self, value, next=None): 
     self.value = value 
     self.next = next 

    def __str__(self): # borrowed from Joran Beasley's answer 
     return "%s->%s" % (self.value, self.next) 


def reverse_linked_list(lst, tail=None): 
    if lst is None: 
     return tail 
    else: 
     return reverse_linked_list(lst.next, Node(lst.value, tail)) 

def swap_23_linked_list(lst): 
    try: 
     second = lst.next 
     third = second.next 
    except AttributeError: # probably lst or lst.next is None! 
     raise ValueError("list is too sort to swap second and third values") 

    new_third = Node(second.value, third.next) # reuse all the nodes past third! 
    new_second = Node(third.value, new_third) 
    new_first = Node(lst.value, new_second) 

    return new_first 

사용 예제 :

>>> list_1 = Node(3, Node(1, Node(4, Node(2)))) 
>>> print(list_1) 
3->1->4->2->None 
>>> list_2 = reverse_linked_list(list_1) 
>>> print(list_2) 
2->4->1->3->None 
>>> list_3 = swap_23_linked_list(list_2) 
>>> print(list_3) 
2->1->4->3->None