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

create Linkedlist from list

create Linkedlist from list Question: Can anyone help me understand why I get this error ? I design a Linkedlist Class like this where I want to create a linkedlist from an array: class Node: def __init__(self, val, next=None): self.val = val self.next = next class LinkedList: def __init__(self, head=None): self.head = head def arrayToLL(X): …

Total answers: 3

Linked list- current vs. current.next when appending

Linked list- current vs. current.next when appending Question: Node: class Node: def __init__(self, data): self.data = data self.next = None def add() part one: def add(self, newData): if self.head is None: self.head = Node(newData) return current = self.head def add() part 2 Why does this work? while current.next: current = current.next current.next = Node(newData) And …

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

A linked list as a tuple Python

A linked list as a tuple Python Question: A connected list is given, which is implemented as a tuple (a number, link to the following pair) of the form in which the values are already sorted: x = (1, (3, (4, (7, (9, None) It is necessary to implement a function that reverses the list: …

Total answers: 2

Solving Leet Code "876. Middle of the Linked List", easy level

Solving Leet Code "876. Middle of the Linked List", easy level Question: I have written this solution, that looks similar to the official one, but I don’t understand why it doesn’t work. My solution: class Solution: def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: arr = [] l = 0 while head: arr.append(head) l += 1 head.next …

Total answers: 1

Reversed double linked list by python

Reversed double linked list by python Question: why can’t print reversed this double linked list by python? always print 6 or None please can anyone help me fast to pass this task /////////////////////////////////////////////////////////////////////////// class Node: def __init__(self, data=None, next=None, prev=None): self.data = data self.next = next self.previous = prev sample methods==> def set_data(self, newData): self.data …

Total answers: 1