All Questions
Tagged with python singly-linked-list
188 questions
-3
votes
3
answers
46
views
What is the difference when assigning an object like a LinkedList to a variable versus an array?
I was working on this problem from HackerRank trying to insert an element at the tail of a linked list and I have to return the head of the node.
def insertNodeAtTail(head, data):
if head is None:...
1
vote
2
answers
79
views
Alternate linked list merge
I wanted to merge 2 linked list’s elements alternatively. I counted the length of these 2 linked lists manually, then created a dummy-headed node to use for the merge. I am getting the errors "...
0
votes
1
answer
54
views
I'm having a problem with insertion in a singly Linked List on python
So, I was coding a linked list in python using classes and after succesfullly defining and running all of the methods I setted up for the class, I decided to create an "insert" method, where ...
0
votes
1
answer
39
views
Deletion of node from linkedlist
I have created a linkedlist class with deletion method. I have written the code to delete the node no matter what situation it is, like
if deleting node is self.head
if only one node present in ...
1
vote
0
answers
57
views
Tuple unpacking issue in reversing a linked list using tuple unpacking in Python [duplicate]
Reversing Linked List Leetcode-206
Initial Approach:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseList(self, head: Optional[...
-1
votes
3
answers
92
views
Python - LinkedList confusion
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def ...
-1
votes
2
answers
2k
views
LeetCode - 2. Add Two Numbers
I'm attempting to address Leetcode problem 2. Add Two Numbers:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of ...
-1
votes
1
answer
81
views
AttributeError: 'LinkedList' object has no attribute 'insert_head'
I tried to insert elements in head in a single linkedlist
I don't understand what is wrong with my code. it says:
AttributeError: 'LinkedList' object has no attribute 'insert_head'
Here is my code:
...
0
votes
1
answer
126
views
Find kth node from the end
I written below function to find kth node from the end. However, one hidden test case is failing.
Please let me know what is wrong with below code.
def find_kth_from_end(l, k):
slow = l.head
...
0
votes
0
answers
39
views
why am i not able to assign and getting an error AttributeError: 'NoneType' object has no attribute 'next' slow.next=fast.next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
temp=head
cnt=0
while temp:
temp=temp.next
cnt+=1
m=cnt-n
...
1
vote
0
answers
62
views
error with reversing linked lists in Python
I've been trying to write a code that will reverse the linked list and I think I've got the concept of reversing the linked list down but I cannot put it into code.
the ideal outcome should be
4
3
2
1
...
1
vote
2
answers
34
views
Does python multivariable assignment order mattering?
I am working on leetcode problem 206 and have come up with :
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
...
-3
votes
2
answers
120
views
I am traversing the linked list in Python and when I am passing arguments, it's giving me an error. How can I take objects to see the result?
Consider:
class Node:
def __init__(self,data):
self.data=data
self.ref=ref
class LinkedList:
def __init__(self):
self.head= None
def print_LL(self):
if self.head==...
1
vote
2
answers
245
views
Modifying the value in a singly linked list at specified index
I'm trying to implement a method in my linked list called set_value() where the value gets modified at that index. However, my code does not reflect the change whenever the function is called. I'm not ...
1
vote
2
answers
79
views
How to Add 1 to a number represented as linked list using Python In the below code format?
I have a linked list with 1999 which I reversed to get 9991. However I am getting None as an output when calling add_one function for the below singly linked list coding exercise. I supposed to be ...