linked-list

Python – linked list

Python – linked list Question: Why is it only printing 6 and 7? I need to print all the values. I believe there is some issue with get() as self.head is being updated somewhere? If so, how can I declare self.head here as the beginning so that it prints all the elements. Thanks in advance. …

Total answers: 1

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

Time complexity regarding linked_list ( Recursion )

Time complexity regarding linked_list ( Recursion ) Question: let us say I have a recursive call for linked list: ( Singular ) def append(self, val): def append_rec(node, val): if node.next is None: node.next = Node(val) else: append_rec(node.next, val) if self.head is None: self.head = Node(val) self.len += 1 else: append_rec(self.head, val) self.len += 1 what …

Total answers: 1

Return Statement from a method/function

Return Statement from a method/function Question: I feel a little bit silly asking this, but I’m practicing some DSA. I’m watching a Youtube video, and I find odd that in some methods of the class the return method is left without returning a value. I know that it’ll return None but I don’t understand how …

Total answers: 3

(Python) Help decoding what is happening here

(Python) Help decoding what is happening here Question: This code is not mine, I am just trying to understand it! I need some help understanding what is happening in this lst2link function. So the function starts off by creating two ListNodes in the variables cur and dummy and the then proceeds to build the linked …

Total answers: 2

What is wrong in my code… Split A circular linked list

What is wrong in my code… Split A circular linked list Question: Question https://practice.geeksforgeeks.org/problems/split-a-circular-linked-list-into-two-halves/1 Given a Cirular Linked List of size N, split it into two halves circular lists. If there are odd number of nodes in the given circular linked list then out of the resulting two halved lists, first list should have one …

Total answers: 3

AttributeError: 'list' object has no attribute 'val' in linked list LeetCode challenge

AttributeError: 'list' object has no attribute 'val' in linked list LeetCode challenge Question: I am trying to solve a LeetCode problem concerning linked lists Merge Two Sorted Lists, but when I test my solution locally, and pass example lists [1,2,3] to my function, I get this error: AttributeError: ‘list’ object has no attribute ‘val’ What …

Total answers: 1

Inserting and getting the position in a Linked List class using python

Inserting and getting the position in a Linked List class using python Question: I am new to Data Structure and algorithms. I’m a self-taught Python programmer. I am doing a course on it and I wanted to make a linked list, get a specific position in the linked list, insert, and delete an element in …

Total answers: 1

When does a pointer to a linked list change the actual list?

When does a pointer to a linked list change the actual list? Question: I have a singly-linked-list, L, and create a pointer to this list P. It seems like sometimes modifying P changes the actual list, while other times modifying P does nothing to the actual list L and only changes what P is pointing …

Total answers: 3

Explanation about dummy nodes and pointers in linked lists

Explanation about dummy nodes and pointers in linked lists Question: I have the following class for a list node: def __init__(self, x): self.val = x self.next = None If I initialize the lists l and r as follows: l = ListNode(1) l.next = ListNode(4) l.next.next = ListNode(5) r = ListNode(1) r.next = ListNode(3) r.next.next = …

Total answers: 1