2,667 questions
-3
votes
2
answers
63
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:...
0
votes
2
answers
109
views
How to dynamically create multiple instances of struct in loop in C? [duplicate]
I have the following code to create one-way list of user-specified number of elements
typedef struct Node{
int val;
struct * Node next;
} Node;
int main(){
int num;
Node * prev = NULL;...
0
votes
3
answers
73
views
Why doesn’t setting a linked list node's next pointer to null also affect the node it was pointing to?
I’m attempting to reverse a singly linked list in Java, but I'm running into confusion about how references work during the reversal process. Specifically, I don’t understand why setting the next ...
-1
votes
1
answer
65
views
writing pop from end function in rust
This is my singly linked list code have to implement pop from end and pop from front functions still, but I'm having difficulty in writing pop from end function
#![allow(warnings)]
#[derive(Clone)]
...
1
vote
3
answers
235
views
Deleting one node of single linked list use double pointers in C language
I have been reading something related to C language's double pointers, and I have some doubts about the following piece of code. The following piece of code is about the operation of deleting a node ...
5
votes
3
answers
166
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
64
views
Error in using call by reference with function to change the head of a singly linked list
I'm learning linked lists in C, and have come across an error when trying to add a value at beginning of a linked list using functions
#include<stdio.h>
#include<stdlib.h>
struct node{
...
-1
votes
1
answer
56
views
how to make a linked list with template
i have problems with distributing template. i tried with different syntax, but not a successful way with the error text:
error C2512: 'node': no appropriate default constructor available
for the code
...
1
vote
2
answers
101
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
5
answers
105
views
a heap-use-after-free wrong in the question--Design MyLinkList (LeetCode No.707)
Below is my C code for LeetCode no. 707 "Design Linked List":
typedef struct MyLinkedList{
int val;
struct MyLinkedList *next;
} MyLinkedList, *LinkList;
MyLinkedList* ...
1
vote
1
answer
110
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 ...
0
votes
1
answer
98
views
Why the structure pointer "p" in the following code is not updating with the "temp" value assigned to it? [duplicate]
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
};
int main(int argc, char *argv[]) {
struct node *p;
p = NULL;
Append(&...
-2
votes
2
answers
49
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?
0
votes
2
answers
270
views
Is using free(p->next) acceptable in C?
I'm working on solving a simple algorithm problem, and here's the question.
In a singly linked list L with a head node, delete all nodes with the value x and free their space. Assume that nodes with ...
-6
votes
1
answer
183
views
Merge sort for Linked List
The merge sort code on Leetcode giving stack-overflow error.
ListNode *findMiddle(ListNode *head){
if (!head) return nullptr;
ListNode *slow=head;
ListNode *fast=head;
while (fast!=...
1
vote
2
answers
137
views
How to get rows of a csv file into a linkedlist iteratively?
I am asked to extract rows of a csv file into a linkedlist, every node of the linkedlist will store one row of the data, where the "data" pointer inside the linkedlist points towards a ...
1
vote
2
answers
121
views
I don't understand how this function works, which is reverse() function in implementation of a linked list
Here is code for implementing a singly-linked list:
class LinkedList {
constructor(value) {
this.head = {
value: value,
next: null,
};
this.tail = this.head;
this.length =...
0
votes
2
answers
70
views
Shift method in a singly linked list
I have this singly linked list:
class Node{
constructor(val){
this.val = val;
this.next = null;
}
}
class SinglyLinkedList{
constructor(){
this.head = null;
...
1
vote
1
answer
141
views
Mergesort for singly-linked lists gives correct results but leaks memory
I'm working on an assignment to implement mergesort for singly-linked lists in C++. The merge function needs to merge two sorted lists in-place without creating new nodes. The mergesort function ...
0
votes
2
answers
747
views
Reverse singly linked list in C and return new head
Please, what am I missing? I am trying to reverse a singly linked list (in-place), but all I get returned is the one node (pointer to the head of the list). I've tried using a void function (as a ...
-1
votes
1
answer
105
views
Segmentation fault in my code SLL Natural Merge Sort in C++
This is my code SLL Natural Merge Sort in C++:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
Node* link;
}NODE;
typedef struct List{
NODE* first;
...
2
votes
3
answers
83
views
Why does this Linked List C function not work properly if previousNode->next is not set to NULL?
Node* deleteAtTail(Node* head)
{
if (head == NULL) /* If List is empty... */
{
return NULL;
}
else /* ...
-1
votes
1
answer
59
views
Reversing a linked list-Find error in existing logic
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ...
-1
votes
1
answer
111
views
Singly linked list returning an error even though I have already checked it [closed]
When using a singly-linked list, I've been suggested to use "if x != nullptr" but I don't understand why this works. In my example code, the first and second print statements work, but as ...
0
votes
1
answer
59
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 ...
-1
votes
2
answers
42
views
Issue in carryovers while adding two numbers in a linked list
I'm solving the leetcode question for adding two numbers in a linked list.
The following is my code:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *...
1
vote
2
answers
184
views
Why I'm hitting time limit!? LeetCode Linked List Cycle (Solved but explanation needed!)
I'm working on solving LeetCode problem 141. Linked List Cycle:
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is ...
-1
votes
4
answers
113
views
Incompatible pointer types when declaring a struct [duplicate]
list.h: In function ‘CreateNewLinks’:
list.h:29:20: warning: assignment to ‘numbers *’ from incompatible pointer type ‘struct numbers *’ [-Wincompatible-pointer-types]
29 | linked = ...
1
vote
1
answer
316
views
Can we reverse the elements in a Singly Circular Linked List using only two pointers? Is that possible, efficient and what is the time complexity?
I need to know about only one thing clearly, as I had tried the singly circular linked list reversal using the C language. In that I can't find the correct way and I have no idea regarding this one. ...
0
votes
1
answer
81
views
Stuck on the Case 3 of Leetcode "Add two Sums"
Currently I'm doing a daily Leetcode streak and I have no idea why my code couldn't pass the case 3. (I have done modifying and tracing the code, but maybe I'm a bit dumb and couldn't spot the error.) ...
1
vote
1
answer
44
views
Shuffle merging two linked lists with the same number of nodes
I have two functions for merging two linked lists assuming they have the same number of elements
The first one:
def shuffle_merge(self, l1,l2):
node1 = l1.head
node2 = l2.head
...
1
vote
1
answer
152
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 ...
-1
votes
1
answer
228
views
Leetcode 234. Palindrome Linked List, String solution gives time exceeded error, can anyone explain why?
This is the solution i have given.
class Solution {
public boolean isPalindrome(ListNode head) {
String s= "";
String p= "";
while(head != null){
...
0
votes
1
answer
67
views
Is it possible to have a linked list in a list node?
Java linked list API not allowed.
Is it possible to have a linked list inside a list node? If yes, how do I insert data into the linked list that is in the list node?
List Node class (I tried to add a ...
1
vote
2
answers
93
views
How do I free a returned string value that is dynamically allocated in C?
This is my first post on StackOverFlow, I was writing the code for a linked list in C, when suddently stumble upon a situation i have no solution for (located in dupstring function). This is my ...
0
votes
0
answers
21
views
Failed to skip duplicated data in a sorted linked list in toString method [duplicate]
First things first, I am not allowed to use the Java API for linked list, the Linked List and List Node codes are given by my teacher, so it may or may not be the same as the stuff you see in the API.
...
0
votes
1
answer
133
views
Which is the correct approach to delete LinkedList Node?
I have created a Linked List in C. Now I want to delete node from any position for example the first node or last node or any nth node. I wrote a code that worked fine. But problem is that someone ...
0
votes
1
answer
46
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 ...
0
votes
1
answer
53
views
Printing a linked list in a reversed spiral manner
Given a Linked list, the task is to print a singly linked list in a spiral fashion. Starting from the first node then the last node followed by the second node and then the second last node, ...
0
votes
1
answer
68
views
Linked-List nth insertion fails
I have wrote a code to insert data in LinkedList. But there is problem outputting the data. Here is the code.
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <...
0
votes
0
answers
49
views
Lost Data in the final node while create Singly Linked List in Java
Hi there. Thank you all for your comments. The problem solved and I also learn something new about editing and formatting. I want to thumb up every comment and close the post but I did not figure out ...
1
vote
2
answers
148
views
Java: Inserting node at the end of a LinkedList
I am currently learning Java and data structures, and I am trying to use insertion to insert the values in a double array to a LinkedList by only inserting each element at the end of the list. I've ...
0
votes
1
answer
63
views
Why does a segmentation fault appear with this data structure?
Given the datastructure
#define DATASTRUCTURE_H
#define MAXINDEX 307
typedef enum {So, Mo, Di, Mi, Do, Fr, Sa} eDayofTheWeek;
typedef struct{
int Day;
int Month;
int Year;
...
0
votes
3
answers
137
views
Difference in these Linked List Pointer declarations
What is the difference between
struct LinkedList *current = malloc(sizeof(struct LinkedList));
and
struct LinkedList *current;
where
struct LinkedList{
int data;
struct LinkedList *next;
}
When I ...
1
vote
1
answer
93
views
valid Parenthese using stack in c
I was solving the problem 20. Valid Parentheses in Leetcode using c and when they check this input s ="{[]}" the code returns false instead of true but I don't see any problem with the code:...
0
votes
0
answers
130
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
1
answer
51
views
Conceptualizing a linked-list post reversal
I'm seeking clarification on how to properly understand a singly linked list once it has been reversed. Typically, the first element of the list should always be stored at the head of the linked list....
0
votes
3
answers
109
views
CompareTo Method: Return Type: GREATER, EQUAL, LESS
I'm receiving these 4 errors:
character expected errors
character expected errors
I'm doing a LinkedList java project that requires the implementation of the CompareTo method. I am rusty on Java and ...
0
votes
2
answers
115
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
59
views
Created function that pops the last value entered in the stack fails after second popping
struct n {
int data;
struct n* next;
};
typedef struct n node;
node* push(node* s, int x) {
node* temp;// temporary element
temp = (node*)malloc(sizeof(node));
if (temp == NULL) {...