linked-list

Print a singly-linked list backwards, in constant space and linear time

喜你入骨 提交于 2019-12-03 13:23:05
I heard an interview question: "Print a singly-linked list backwards, in constant space and linear time." My solution was to reverse the linkedlist in place and then print it like that. Is there another solution that is nondestructive? If you reverse it again after printing it will no longer be destructive, since the original order is restored. You've already figured out most of the answer: reverse the linked list in place, and traverse the list back to the beginning to print it. To keep it from being (permanently) destructive, reverse the linked list in place again as you're traversing it

How does LinkedList work internally in Java?

ぐ巨炮叔叔 提交于 2019-12-03 12:41:13
As far as I know, the concept of a linked list is a bunch of object connected to each other by having a 'next' and sometimes 'previous' attribute to traverse the objects. I noticed in Java, you can create a LinkedList object...but treat it like an array/list/sequence by using the same methods such as .add(), .get(), etc. So, is LinkedList internally an array-like sequence? Michael Borgwardt So, is LinkedList internally an array-like sequence? No. It's a series of instances of a private nested class Entry , which has next , previous and element references. Note that you could have found this

Why no immutable double linked list in Scala collections?

感情迁移 提交于 2019-12-03 12:20:48
Looking at this question, where the questioner is interested in the first and last instances of some element in a List , it seems a more efficient solution would be to use a DoubleLinkedList that could search backwards from the end of the list. However there is only one implementation in the collections API and it's mutable. Why is there no immutable version? Because you would have to copy the whole list each time you want to make a change. With a normal linked list, you can at least prepend to the list without having to copy everything. And if you do want to copy everything on every change,

Array/Linked list: performance depends on the *direction* of traversal? [closed]

时光怂恿深爱的人放手 提交于 2019-12-03 11:50:33
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . This post is divided to two major sections. The first section introduces the original test cases & results, and my thoughts about it.

Creating a node class in Java

萝らか妹 提交于 2019-12-03 11:38:32
So I'm fairly new to Java and programming and I was wondering how to create a node class? So far I have: public class ItemInfoNode{ private ItemInfoNode next; private ItemInfoNode prev; private ItemInfo info; public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev){ info = info; next = next; prev = prev; } public void setInfo(ItemInfo info){ info = info; } public void setNext(ItemInfoNode node){ next = node; } public void setPrev(ItemInfoNode node){ prev = node; } public ItemInfo getInfo(){ return info; } public ItemInfoNode getNext(){ return next; } public ItemInfoNode getPrev

Scala 2.11 LinkedList is deprecated, what should I use?

风流意气都作罢 提交于 2019-12-03 10:44:52
According to the docs , scala.collection.mutable.LinkedList is deprecated as of the 2.11 version. Unfortunately I have found nothing to replace it with. I need an ordered collection that can remove an item from any index in constant time. What should I use? Use MutableList and its iterator's remove method. They provide O(1) removal. http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html#linked_lists From what I understand of your problem you want to iterate through collection and change it on the fly. That is not possible with collection constructs other than

Are vector a special case of linked lists?

本小妞迷上赌 提交于 2019-12-03 10:03:47
问题 When talking about the STL, I have several schoolmates telling me that "vectors are linked lists". I have another one arguing that if you call the erase() method with an iterator, it breaks the vector, since it's a linked list. They also tend to don't understand why I'm always arguing that vector are contiguous, just like any other array, and don't seem to understand what random access means. Are vector stricly contiguous just like regular arrays, or just at most contiguous ? (for example it

Difference between LIST_HEAD_INIT and INIT_LIST_HEAD

断了今生、忘了曾经 提交于 2019-12-03 09:45:22
问题 I'm trying to understand the Linux kernel linked list API. According to Linux Kernel Linked List I should initialize the list head by INIT_LIST_HEAD but here (Linux Kernel Program) it's suggested to use LIST_HEAD_INIT instead. Here's a working code I wrote, but I'm not sure if I did it in proper way. Could someone verify that it's OK? #include <stdio.h> #include <stdlib.h> #include "list.h" typedef struct edge_attr { int d; struct list_head list; } edge_attributes_t; typedef struct edge { int

Flatten binary search to in order singly linked list [C]

怎甘沉沦 提交于 2019-12-03 08:32:31
I am trying to flatten a binary search tree to a singly linked list. Binary search tree: 6 / \ 4 8 / \ \ 1 5 11 / 10 Flattened Singly Linked List: 1 -> 4 -> 5 -> 6 -> 8 -> 10 -> 11 I can't seem to figure this out for some reason. I have a struct for the tree nodes: typedef stuct node { int key; struct node *left; struct node *right; } Node; I have a function to create and allocate memory to the tree node: Node* newNode (int key) { Node *new = malloc (sizeof(Node)); new->left = NULL; new->right = NULL; new->key = key; return new; } I have a struct for the list nodes: typedef struct list { int

dealing with array of linked list

二次信任 提交于 2019-12-03 08:24:23
My approach: An array of fixed-length (lets say 20) each element is pointer to the first node of a linked list. so i have 20 different linked list. This is the structure: struct node{ char data[16]; struct node *next; }; My declaration for that array struct node *nodesArr[20]; now to add a new node to one of the linked list, i do this: struct node *temp; temp = nodesArr[i]; // i is declared and its less than 20 addNode(temp,word); // word is declared (char *word) and has a value ("hello") The addNode function: void addNode(struct node *q, char *d){ if(q == NULL) q = malloc(sizeof(struct node))