Skip to main content

Questions tagged [linked-list]

A linked list is a data structure in which the elements contain references to the next (and optionally the previous) element.

Filter by
Sorted by
Tagged with
3 votes
3 answers
237 views

Simple imperative linked list

I've decided to learn a bit of C++, and so I've started with a simple linked list. There's no input to the program and should always output hlo. This is as in ...
Peilonrayz's user avatar
  • 43.4k
3 votes
3 answers
245 views

Is SList class an ADT?

As per the definition given in lecture, ADT is, With reference to this definition, I would like to call SList class an ADT following 6 invariants: Multiple users ...
overexchange's user avatar
  • 3,341
3 votes
2 answers
8k views

Insert a node at a specific position in a Linked List

This code is built to insert a Node into a specific position of a linked-list. It passes all the test cases for the interview, but I was wondering about the coding ...
nathan's user avatar
  • 131
3 votes
3 answers
170 views

Singly Linked List in pure C

I recently learned about linked lists and here is my first try to write one in C. I know it's a lot of code, but I hope someone takes a look. Any tips memory allocation and improvements are welcome. ...
LastSecondsToLive's user avatar
3 votes
3 answers
242 views

Data Structures in C (Single Linked List)

NOTE: Added a follow-up question with the reviewed code I am trying to get back to basics with C, so I am doing some data structures in it. The following is my implementation of a Single Linked List ...
AntonioCS's user avatar
  • 536
3 votes
3 answers
1k views

Linked List design and implementation - C++

Seeking recommendations on how to improve this code, LL always seem to be a struggle. A basic linked list that does include next and previous pointers. I used a struct inside of a class, which I'm ...
user111415's user avatar
3 votes
1 answer
6k views

Generic Stack (Array and Linked List) Implementation

I am working on brushing up my data-structures knowledge, and was hoping to have my code/thoughts reviewed. Thoughts on Generic Array Stack: Insertion/Pop are amortized to \$\mathcal{O}(1)\$. This ...
user3610520's user avatar
3 votes
2 answers
728 views

Merge sorting a singly-linked list in Java

I have this nifty merge sort implementation for sorting singly-linked lists. Its running time is \$\Theta(n \log n)\$, yet it uses only \$\Theta(\log n)\$ space (the stack). See what I have below. ...
coderodde's user avatar
  • 28.7k
3 votes
3 answers
418 views

Merge sorting a singly-linked list in C - follow-up

(See the initial iteration.) Continuing from Merge sorting a singly-linked list in C, I have incorporated the points made by @vnp. Now it looks like this: ...
coderodde's user avatar
  • 28.7k
3 votes
3 answers
124 views

Linked list implementation - follow-up

Follow-up to this question as recommended. Can this code be improved from an OOP paradigm perspective? ...
overexchange's user avatar
  • 3,341
3 votes
2 answers
137 views

Generic Double Linked List Follow up

This is a continuation from the previous post found here. I made most of the changes that was suggested. I just want to see if there are any additional changes I should make to my code. Here is my ...
Snorrlaxxx's user avatar
3 votes
2 answers
769 views

SPOJ Class Leader using circular linked list

I have solved a spoj question CLASS LEADER. For each test case there are n students and a paper will be given to student m and now the game starts, the student m will pass the paper by o positions ...
user avatar
3 votes
1 answer
105 views

Creating a generic single linked list, follow up

This is a thread I started from here. I organized my code to make it more organized. Everything works well, but I have not made all the changes from the last link (namely namespace since I am exactly ...
Snorrlaxxx's user avatar
3 votes
1 answer
940 views

Designing function prototypes for a singly-linked list API in C

I am in the process of rewriting in C all the data structures that I learned about a few years ago to increase my understanding of data structures and the C language. The first one I'm doing is singly-...
oddlogic's user avatar
3 votes
1 answer
40k views

Deleting a linked list node

This is a classic linked list problem. Deleting a node from linked list given the data to be deleted. Inserting a node in a sorted linked list. I saw various versions of this and here is my version. ...
user avatar
2 votes
3 answers
17k views

Linked list remove() and free()

How can I improve my remove function? Could someone also give me a solid explanation of the free() function? Here is my remove function: ...
CodeKingPlusPlus's user avatar
2 votes
2 answers
1k views

Linked List Queue Implementation

I implemented a Queue using linked list data structure. This is also my first time using templates in C++. OVERVIEW Queue is a data-structure that supports only ...
theProgrammer's user avatar
2 votes
2 answers
77 views

remove kth last element from singly-linked list - Follow up

This code is a revised version of implementation which asked for an improvement. Original question is asked here: remove kth last element from singly-linked list credits to: Toby, Andreas, Arkadiusz ...
Erdenebat Ulziisaikhan's user avatar
2 votes
1 answer
213 views

Double LinkedList Deep Copy in Kotlin

Goal Return a deep copy of a double LinkedList. Each node also contains an additional random pointer, potentially to any node or null. Code to start ...
AdamHurwitz's user avatar
2 votes
1 answer
678 views

Optimizing twin() method

Part III of this question says: Fill in the twin() method in the SList class so that it performs as indicated in the ...
overexchange's user avatar
  • 3,341
2 votes
1 answer
1k views

Extract Leaves of a Binary Tree in a Doubly Linked List

Given a Binary Tree, extract all leaves of it in a Doubly Linked List (DLL). Note that the DLL need to be created in-place. Assume that the node structure of DLL and Binary Tree is same, only the ...
JavaDeveloper's user avatar
2 votes
1 answer
72 views

Printing heads of stand-alone lists

Please review my code to help improve its quality. ...
JavaDeveloper's user avatar
2 votes
1 answer
103 views

Populate the list based on which datacenter code is running on

This is a follow on to : Efficiently detect datacenter based on server hostname and return the full path. I have a library which is running in different datacenters in production. I have three ...
david's user avatar
  • 1,947
2 votes
1 answer
83 views

Faster, indexed, heuristic doubly-linked list data structure in Java: benchmark

I have this benchmark program for my indexed linked list. It looks like this: com.github.coderodde.util.benchmark.LinkedListBenchmarkRunner ...
coderodde's user avatar
  • 28.7k
2 votes
2 answers
1k views

Partition a singly linked list using Java

This is my attempted solution to exercise 2.4 in Cracking the Coding Interview. The problem statement is: Write code to partition a linked list around a value x such that all nodes less than x ...
Average's user avatar
  • 841
2 votes
1 answer
2k views

Python implementation as single linked list class `replace item` with item

I encountered this question as I am preparing for my code interview. I was implementing my linked list implementation. I would like to ask for the following implementation: Replace item with item ...
NinjaG's user avatar
  • 2,469
2 votes
2 answers
132 views

My own expandable list

I wondered how an expandable list could be implemented. My first thought: You could create a class that has an array and you can create a new array each time the user wants to append an element and ...
Dexter Thorn's user avatar
  • 2,174
2 votes
4 answers
978 views

Removing nodes with a given value from a linked list

Is this a good way of removing all the nodes that contains a value x? This function takes as input the head of a list and has to delete all the nodes that contain a given value taken. If there is ...
awwwww's user avatar
  • 53
2 votes
1 answer
2k views

Generic Macro Generated Linked List in C

Generate a linked list of any type using macros. The linked list contains functions for adding and removing elements to both ends and also at the middle. You can also add elements relatively to a list ...
LeoVen's user avatar
  • 337
2 votes
0 answers
151 views

Faster k-shortest path algorithm in Java

In the \$k\$-shortest path problem, we are searching for (at most) \$k\$ distinct, shortest paths connecting two given terminal nodes. You can see the previous version here. This implementation, ...
coderodde's user avatar
  • 28.7k
2 votes
1 answer
661 views

Linked list implementation along with unit test - Round 3

Here are the changes I've made thanks to suggestions made by @vnp, and @Henrik Hansen. Here is the link to the previous code review: Linked list implementation along with unit test - Round 2 I've ...
feature_creep's user avatar
2 votes
1 answer
114 views

Singly linked list in pure C - follow-up

This is an update for this post. I fixed all style-issues from the comments, but still have a few questions left: Someone commented that my last post contained too many functions, which could be ...
LastSecondsToLive's user avatar
2 votes
1 answer
6k views

Circular Linked List implementation in C++

I want to improve this code. ...
coder's user avatar
  • 2,449
1 vote
1 answer
83 views

Rotating lists in linear time and constant space (Java) - follow-up

(The previous and the initial iteration.) After some critique, I found the way to keep the rotation operation also for java.util.Lists that do not provide random ...
coderodde's user avatar
  • 28.7k
1 vote
1 answer
3k views

Sorted trie implementation in C

I wanted to store some dictionary data (key/value pairs of strings) in C, and decided to use a trie. I found what looks like a good implementation here, along with some nice notes. In this ...
Dagg's user avatar
  • 4,531
1 vote
1 answer
594 views

Linked List Interview Code methods, runtime, and edge cases

Refactored code here I want to write a very simple Linked List with only 3 methods Append, Remove and Print. This is not for production and is to be treated as code that could be used in an interview ...
greg's user avatar
  • 1,017
1 vote
1 answer
1k views

Rotating a matrix in Java

Introduction This program is about rotating a matrix. For example, let the matrix be abcde fghij klmno pqrst Now, after rotating two steps clockwise we will ...
coderodde's user avatar
  • 28.7k
1 vote
1 answer
21k views

Doubly circular link list implementation

I am supposed to be creating a doubly circular linked list, but I am now have problems creating the actual linked list. Is there a problem with the classes? Also, for the assignment, the list is ...
Anthony Nesbitt's user avatar
1 vote
1 answer
518 views

Working C code dealing with lists: problem with memory leak

I have written a working code that generates a simple linked list of dates and associated holidays. I realize that I have a problem with memory leaking as I copy lists repeatedly, so I wrote a ...
sammis's user avatar
  • 113
1 vote
1 answer
953 views

Delete node from singly linked list with tail pointer in C

I'm trying to implement a single linked list based on Linus' comments about double pointers. In my case I'm also using a tail pointer and I'm wondering if there's a better way to update the tail ...
LiamRyan's user avatar
  • 313
1 vote
4 answers
874 views

Queue using doubly linked list

I have implemented my queue based on doubly linked list. The codes is tested and works, however, I am interested in some improvement ideas, thanks Based on some review remarks on my previous ...
V_head's user avatar
  • 525
1 vote
1 answer
268 views

OOP paradigm implementation of a linked list

Can this code be improved from an OOP paradigm perspective? ...
overexchange's user avatar
  • 3,341
1 vote
2 answers
224 views

LinkedList Implementation in Java

Please review my implementation. I know there is already an implementation in Java, but I just wanted to come up with my implementation. Let me know if I can improve this. ...
Sandeep Kumar's user avatar
1 vote
1 answer
108 views

Revision-2 C++ implementation of a linked list

I have addressed all your comments in my previous post. C++ implementation of a linked list Can you please comment on, How efficient the code is? Am i using too much extra variables to implement ...
user1762571's user avatar
1 vote
2 answers
1k views

Check if a linked list is a palindrome in O(n) time and O(1) space

I liked this problem because there are so many sub-problems that are interview problems in their own right-- (reverse linked list, find midpoint) ...
WinchenzoMagnifico's user avatar
1 vote
1 answer
79 views

Reverse a sublist of a singly-linked master list in constant space and maximum of one pass (Java)

So I found this funky programming challenge somewhere on Quora. The idea is to take the head of a singly-linked list, and reverse a specific sublist. The requirements are: runs in constant space, ...
coderodde's user avatar
  • 28.7k
0 votes
2 answers
99 views

Singly Linked List Implementation (With Nodes)

This code was recommended to me by my dear friend Jon after I wrote an implementation using arrays. With his help, I managed to create a Singly linked List similar to std::vector using Nodes. ...
ChubakBidpaa's user avatar
0 votes
2 answers
228 views

remove kth last element from singly-linked list

I have implemented a code to solve following problem. I would like to improve the code efficiency. Pardon if I just printed out the result. Code has O(N) time and constant space complexity. Given a ...
Erdenebat Ulziisaikhan's user avatar
0 votes
2 answers
6k views

C++ remove from singly linked list

I asked a similar question (C++ linked list) but I'd like to have this similar code reviewed. It's not the same code, just slightly different and with bugs fixed. The code takes the head pointer of a ...
Floella's user avatar
  • 207
0 votes
2 answers
444 views

Merge sorting a singly-linked list in C

(See the next iteration.) I have this C implementation of the merge sort for sorting singly-linked lists: ...
coderodde's user avatar
  • 28.7k