2,665 questions
2
votes
3
answers
127
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;
...
0
votes
0
answers
22
views
BGI application: Deleted node not being deleted entirely, raising error while printing out
Greeting everyone. I am developing a library management application for my DSA class. We use the Borland graphics.h library to draw the user interface (as can be seen from this image here). As from ...
1
vote
1
answer
53
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
45
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
51
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
79
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
92
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
96
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
39
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
votes
2
answers
256
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 ...
-7
votes
1
answer
173
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
98
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
101
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
55
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
127
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
348
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
94
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
70
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
51
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
93
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
52
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
35
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 *...
0
votes
0
answers
34
views
How to Query a Table of Linked Lists with no List ID?
I have a postgres table that's effectively stores linked lists, but none
of the rows have a list identifier. So it's hard to pick out lists from table,
other than to "hop" to each node.
...
1
vote
2
answers
105
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
91
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
227
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
66
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
34
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
122
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
167
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
54
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 ...
2
votes
2
answers
81
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
0
answers
11
views
How can I pass information from one html page to another with simply linked lists?
Hi how are things? Good evening, I hope you are well.
I have a question about Simple Linked Lists.
How can I make it so that on an HTML page (worker.html) through the registration of a car, a button ...
0
votes
1
answer
77
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
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 ...
0
votes
1
answer
42
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
64
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
45
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
112
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
58
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
125
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
69
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
99
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
30
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
81
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
74
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
52
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) {...
1
vote
1
answer
62
views
Don't understand the error in appending a new item to a Linked List (At Beginning and End)
As mentioned I don't seem to be able to understand the error in my code, what is it that I'm doing wrong in this particular piece of code:
The Node is written as such:
struct Node {
int data;
...
1
vote
0
answers
55
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[...