I have this assignment for uni that I have been working on for some time now, but I can’t seem to figure out what’s wrong. The goal is to merge two already-sorted linked lists into one sorted linked list. The only function that I am allowed to change is the merge(a,b) function. I triple-checked, but ..
Category : linked-list
I’m trying the "add two numbers" (linked list) problem in leetcode. The code that I wrote works fine in Jupyter notebook and returns the correct answer: def list_to_digits(lst): lst.reverse() strings = map(str, lst) a_string = "". join(strings) an_integer = int(a_string) return an_integer def Convert_IntegersToList(integers): res = list(map(int, str(integers))) res.reverse() return(res) class ListNode: def __init__(self, val=0, ..
How can I avoid the nested output [(((4, 3), 2), 1)] when trying to build a fully recursive LinkedList reversal? I would like to have as an output [4, 3, 2, 1)] and wonder whether this is possible in Python without iterating over the final result. class Leaf: def __init__(self, value): self.value = value self.next_node ..
I have the following method, and I want to delete {5285831021: ‘Hayes’} from a list. How do I do by just passing 5285831021? class Node: def __init__(self, data=None): self.data = data self.prev = None self.next = None class DoublyLinkedList: def __init__(self): self.head = None self.tail = None def insert(self, pair): if not isinstance(pair, Node): pair ..
How do you access data in a linked list? Mainly, I have two specific questions. I’m a beginner in python and just learning linked lists so bear with me if this seems like a stupid question. I have a linked list of nodes where each node is an instance of a class. How do I ..
I am required to use recursion, I had had it working without recursion, with recursion it fails. class Node: def __init__(self, _item, _next=None): self._item = _item self._next = _next class LinkedList: def __init__(self): self._head = None self._len = 0 These are my classes, below is the code I’m having trouble with. def __contains__(self, item): current ..
class ListNode: # initialize a node def __init__(self, val,next = None): self.val = val self.next = next # allows you to print the list def __str__(self): temp = self arr = [] while temp!= None: arr.append(temp.val) temp = temp.next return "->".join([str(i) for i in arr]) def change_test(link): link = ListNode(5) print(link) # this prints 5 ..
def delete_negative and sumOfNode are both broken and I’m unsure of how to fix them. delete_negative is supposed to go through the linked list and delete any negative numbers. sumOfNode is supposed to return the sum of all values in the linked list. Could someone please walk me through this? class Node: def __init__(self,data): self.data ..
The remove function I’m trying to do is suppose to: Remove the node at the current position if there is one (otherwise does nothing) The node behind the removed node is now in the current position This is what i have so far, but it just removes the first item. Not the node at the ..
Here is a question from leetcode: Define a function, input the head node of a linked list, invert the linked list. example: input: 1->2->3->4->5->NULL output: 5->4->3->2->1->NULL Here is the official answer to it: # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class ..
Recent Comments