All Questions
102 questions
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[...
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
...
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
...
-2
votes
1
answer
114
views
Reversing a linked list in python: What is wrong in my code?
I am trying to reverse a linked list in python. Below is my code which doesn't work (gives an error of 'NoneType' object has no attribute 'next' at the while statement):
class Solution:
def ...
0
votes
2
answers
99
views
Why is my code causing a time limit error, Leetcode 24 ( Linked List)
I am working on LeetCode problem 24. Swap Nodes in Pairs
:
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list'...
0
votes
0
answers
26
views
Issue with code on class nodes in a linked list implementation
I have implemented a linked list using classes in Python. However, I'm facing an issue with the code when it comes to manipulating the nodes. The code doesn't seem to work as expected, specifically in ...
2
votes
1
answer
19
views
How to pass prev_node argument as a Node instance in Linked List insertion method?
I implemented a method to insert an element after a specified node in a Linked List:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
...
0
votes
1
answer
1k
views
How to solve Linked list found cycle error
The code below is written to solve Rotate List problem in leetcode, but my code is throwing "Found cycle in the ListNode" error.
I can't find which line is creating the cycle, help!
# ...
0
votes
1
answer
32
views
Is it possible to use __iter__ in LinkedList __str__ method without try and else?
I wanted to extend karin's answer here where she uses a for loop for loop to print the linked list.
I would like to have __str__ method that is part of SinglyLinkedList but would like to reuse the ...
-1
votes
1
answer
57
views
Why is it necessary to take a new variable "head" in for loop?
class Node:
def __init__(self, data):
self.data = data
self.ref = None
def Print_LL(linkedlist):
if linkedlist is None:
print("LinkedList is empty!!")
...
0
votes
1
answer
860
views
pseudocode to store the elements of a linked list in reverse order
I am working on an algorithm to store the elements of a singly linked list in reverse order inside an array. my tutor suggested the following pseudocode for the problem - everyone in the classroom was ...
0
votes
1
answer
31
views
Two kind of outputs for same code in linkedlist with python
else:
n = self.head
while n.ref is not None:
n = n.ref
n.ref = new_node
** output -> 30 20 10**
if i execute this code o/p is right *
else:
while self....
0
votes
2
answers
125
views
Check if the Linked List is Palindrome or not
When I try to check whether the linked list is palindrome or not, the following code gives me the error. My Approach is:
Step 1:- I am reversing the linked list (temp = reverseLL(head).
Step 2:- ...
-1
votes
1
answer
65
views
Reverse the Linked List using Recursion
I am using recursion to reverse the linked list but not getting the result. Whenever I am using the following code for the input [1,2,3,4,5,6,-1], it shows me the result [1->None]. I am not getting ...