singly-linked-list

Why is it necessary to take a new variable "head" in for loop?

Why is it necessary to take a new variable "head" in for loop? Question: class Node: def __init__(self, data): self.data = data self.ref = None def Print_LL(linkedlist): if linkedlist is None: print("LinkedList is empty!!") else: while linkedlist is not None: print(linkedlist.data) linkedlist = linkedlist.ref def arr_to_LL(arr, n): linkedlist = None for i in range(0,n): new_node …

Total answers: 1

Python : I am not able to prepend a Value in a Singly Linked list

Python : I am not able to prepend a Value in a Singly Linked list Question: Code written in python: prepend Function is supposed to add a value to the start of the Linked List display Function is supposed to display the linked list in a List format in python fromarr Function is supposed to …

Total answers: 2

Issue with Reverse Link List – Leetcode Problem NoneType Error

Issue with Reverse Link List – Leetcode Problem NoneType Error Question: I am attempting to reverse a link list as per this leetcode problem: ReverseLL My code: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def reverseList(self, head): """ :type …

Total answers: 2

Linked List Implementation Error In Python

Linked List Implementation Error In Python Question: So I was trying to make a Linked List in python, but I’m getting this error: If currentNode.nextNode is None: AttributeError: ‘str’ object has no attribute ‘nextNode’ Not sure why I get that as currentNode.nextNode should have a .nextNode attribute just like every other node. Here’s the code: …

Total answers: 2

Returning [None,None] of split array(Python)

Returning [None,None] of split array(Python) Question: Hi i am supposed to learn how to implement the heap sort function. I have a singly linked list (my implementation as well): import numpy as np import math as mt class list_node: def __init__(self,obj,next_listnode): self.obj = obj self.next_listnode = next_listnode and class linked_list: def __init__(self,list_node): self.list_node =list_node def …

Total answers: 2

Linked List Recursive Value Sum

Linked List Recursive Value Sum Question: I am trying to recursively determine the sum of the values in a linked list. I am aware of one recursive solution that works: def sum_list_rec1(head_node: Node): if head_node == None: return 0 return head_node.value + sum_list_rec1(head_node.next) However, I am trying to use the pattern where a variable is …

Total answers: 2

Attribute Error: 'NoneType' object has no attribute '_next'

Attribute Error: 'NoneType' object has no attribute '_next' Question: I understand that these errors occur when plugging instance methods or any preset functions to None type, but since I’m implementing a singly Linked List I need to assign a None type to the last node. How do I resolve this issue since I can’t change …

Total answers: 2

Perform arithmetic on a singly linked list which represent a large number

Perform arithmetic on a singly linked list which represent a large number Question: I have a linked list which represents the large number 2253239487. class ListNode: def __init__(self, val, next=None): self.val = val self.next = next def __repr__(self): return ‘{0}’.format(self.val) The ListNode instance is populated as below: h1 = ListNode(2) n2 = ListNode(2) n3 = …

Total answers: 1