Вот вопрос, на котором я застрял!
Проблема в том, что код не генерирует никакого вывода. (ничего не печатает).
Я предполагаю, что LinkedList
вообще не изменяется!
Я определил только две функции insertAtBeginning(head,x)
и insertAtEnd(head,x)
. Остальной код взят от GeeksForGeeks.
class Node:
def __init__(self,data):
self.data=data
self.next=None
# function inserts data x in front of list and returns new head
def insertAtBegining(head,x):
# code here
global a
if head is None:
a.head = Node(x)
# head.next = None
else:
node = Node(x)
node.next = a.head
a.head = node
# head.next = node
# function appends data x at the end of list and returns new head
def insertAtEnd(head,x):
# code here
global a
if head is None:
a.head = Node(x)
# head.next = None
else:
node = Node(x)
current = head
while current.next is not None:
current = current.next
current.next = node
# node.next = None
#{
# Driver Code Starts
class Node:
def __init__(self,data):
self.data=data
self.next=None
class LinkedList:
def __init__(self):
self.head=None
def printList(head):
while head:
print(head.data,end=' ')
head=head.next
print()
if __name__ == '__main__':
t=int(input())
for cases in range(t):
n=int(input())
a=LinkedList()
nodes_info=list(map(int,input().split()))
for i in range(0,len(nodes_info)-1,2):
if (nodes_info[i+1]==0):
a.head = insertAtBegining(a.head,nodes_info[i])
else:
a.head = insertAtEnd(a.head,nodes_info[i])
printList(a.head)
# } Driver Code Ends
Надеюсь, я предоставил всю необходимую информацию. Пожалуйста помоги.
О, извини, мой плохой! Редактирую сразу
Похоже, что insertAtBegining
должен вернуть новый заголовок связанного списка, тогда как ваша реализация не выглядит так, как будто она вообще ничего возвращает.
Я не могу определить методы для класса там. Задача состоит в том, чтобы написать две функции для модификации связанного списка. Проверьте блок if-else, в котором вызываются две функции.