All Questions
Tagged with singly-linked-list algorithm
143 questions
-1
votes
1
answer
106
views
Why do I only update the head pointer when deleting the first node in a singly-linked list, and can't use prev->next = head? [closed]
I'm implementing node deletion in a singly-linked list and ran into a conceptual problem regarding deleting the head (first node) of the list. I don't know why prev->next = head is not allowed? Isn'...
5
votes
3
answers
150
views
Having trouble understanding Linked List in C
#define SIZE 100000
// 1. hash_function() is a function that return an int
// 2. snowflake_node
typedef struct snowflake_node {
int snowflake[6];
struct snowflake_node *next;
} snowflake_node;
...
1
vote
1
answer
101
views
Linked List Replacement Function with Head, Tail, and Size Management
I'm working on a SinglyLinkedList class in C++, where I maintain pointers to both the head and tail of the list, as well as an integer size to track the number of nodes. My goal is to implement a ...
-2
votes
2
answers
43
views
Which of these implementations is canonical: storing the head and size variables, or storing the head, tail, and size? [closed]
For a singly linked list, should I store the head and size variables, or the head, tail, and size?
1
vote
1
answer
129
views
What happens to the lost part of a singly Linked list when I am introducing a loop at the middle?
I have created a singly linked list, and this is how it looks after displaying it:
19-->85-->50-->20-->33-->9-->1-->7-->null
I have created a method that can add a node to a ...
0
votes
0
answers
116
views
Writing code for Flattening of a Linked List but unable to accomplish the task
You are given a linked list containing 'n' 'head' nodes, where every node in the linked list contains two pointers:
(1) ‘next’ which points to the next node in the list
(2) ‘child’ pointer to a linked ...
0
votes
2
answers
95
views
linked list, swap nodes
I am trying to solve swap node in pairs (linked list). I have the correct code, but I am stucked while interpreting swapping step.
here is the code:
def swapPairs(head):
pre = ListNode(0)
pre....
0
votes
2
answers
97
views
Trying to recursively reverse a linked list passed as an argument without modifying it, returning a new one
My current implementation works with a list length of less than or equal to two. Anything larger I end up losing the nodes in the middle. How can I scale that return object after the recursive call to ...
0
votes
1
answer
95
views
Confused about how tail recursion works?
I'm using an example of adding two linked lists together from leetcode. The lists are in reverse order and the resulting list must be reversed as well. My code is recursive and it's only faster than ...
0
votes
1
answer
121
views
How to remove a node at a given position in a singly linked list?
I have been trying to create a function that removes a node when given an specific location. If the location is out of range then a message will be displayed other than that it should remove the node.
...
1
vote
3
answers
187
views
LinkedList push() method in JavaScript with a this.tail , unable to understand the reference by value
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class SLL {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(val) {
...
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
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
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
173
views
My method for eliminating the largest sequence of repeated elements on singly linked list does not work
This code is supposed to search through a Node list for the largest sequence of repeated elements and then eliminate it (each Node has connection only to the next element, it is a singly linked list). ...