singly-linked-list

Reversing a singly linked list in C [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-02 10:41:50
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to reverse a singly linked list using only two pointers? This is C code to reverse a linked list. But this isn't producing the desired output. struct node *temp,*prev; while(head->next!=NULL) { temp=prev=head; while(temp->next->next!=NULL) { temp=temp->next; prev=prev->next; } temp=temp->next; temp->next=prev; prev->next=NULL; } What am I missing? 回答1: You don't provide enough informations to have more

Creating a linked list with a for loop

心已入冬 提交于 2019-12-02 09:45:26
Here is my struct struct ListItem{ int data; struct ListItem *next; }; Assuming the first node of the linked list will have data = 0, I want to write a for loop that creates a linked list of size 5 but I'm not sure how to work I tried the following int main(int argc, char* argv[]){ struct ListItem a; a.data = 0; for (int i = 1; i < 5; i++){ struct ListItem *pointer = &a; struct ListItem nextnode; nextnode.data = i; a.next = &nextnode; pointer = pointer->next; } } But the result is a.data = 0 and a.next->data = 4 Don't modify a. Take a temp node starting as a. Make it's next point to the new

What are the space complexities of inits and tails?

非 Y 不嫁゛ 提交于 2019-12-01 18:39:47
TL; DR After reading the passage about persistence in Okasaki's Purely Functional Data Structures and going over his illustrative examples about singly linked lists (which is how Haskell's lists are implemented), I was left wondering about the space complexities of Data.List 's inits and tails ... It seems to me that the space complexity of tails is linear in the length of its argument, and the space complexity of inits is quadratic in the length of its argument, but a simple benchmark indicates otherwise. Rationale With tails , the original list can be shared. Computing tails xs simply

Why does == not work while comparing two object type variables boxed with same int value

浪尽此生 提交于 2019-12-01 17:31:21
While trying to implement a simple singly linked list in C#, I noticed that == does not work while comparing two object type variables boxed with an int value but .Equals works. Wanted to check why that is so. The below snippet is a generic object type Data property public class Node { /// <summary> /// Data contained in the node /// </summary> private object Data { get; set; }; } The below code traverses the singly linked list and searches for a value of type object - /// <summary> /// <param name="d">Data to be searched in all the nodes of a singly linked list /// Traverses through each node

Why does == not work while comparing two object type variables boxed with same int value

感情迁移 提交于 2019-12-01 17:05:10
问题 While trying to implement a simple singly linked list in C#, I noticed that == does not work while comparing two object type variables boxed with an int value but .Equals works. Wanted to check why that is so. The below snippet is a generic object type Data property public class Node { /// <summary> /// Data contained in the node /// </summary> private object Data { get; set; }; } The below code traverses the singly linked list and searches for a value of type object - /// <summary> ///

What is node, node.next and node.next.next in linked-list.?

蹲街弑〆低调 提交于 2019-12-01 13:45:57
I am just curious about node . node.next and node.next. Until now we have read that node has two part in which it store data and address of next node(node.next) then whats about node.next.next. ? Where the address of node.next.next will be stored. I just implemented node.next.next to delete a number from the end of list. Its working well. Butt i don't know about node.next.next?? please help me in understanding of concept of nodes. Here is code in which i implemented node.next.next. public void del_`enter code here`end(){ Node temp = head; while(temp.next.next!=null ){ temp= temp.next; } temp

Creating a singly linked list in C

我与影子孤独终老i 提交于 2019-12-01 03:24:43
问题 I'm trying to create a singly linked list from an input text file for an assignment. I'm trying to do it a little bit at a time so I know my code is not complete. I tried creating the head pointer and just printing out its value and I can't even get that to work, but I'm not sure why. I included the struct, my create list, and print list functions. I didn't include the open file since that part works. typedef struct List { struct List *next; /* pointer to the next list node */ char *str; /*

Reversing a singly linked list iteratively

落花浮王杯 提交于 2019-11-30 10:36:22
Has to be O(n) and in-place (space complexity of 1). The code below does work, but is there a simpler or better way? public void invert() { if (this.getHead() == null) return; if (this.getHead().getNext() == null) return; //this method should reverse the order of this linked list in O(n) time Node<E> prevNode = this.getHead().getNext(); Node<E> nextNode = this.getHead().getNext().getNext(); prevNode.setNext(this.getHead()); this.getHead().setNext(nextNode); nextNode = nextNode.getNext(); while (this.getHead().getNext() != null) { this.getHead().getNext().setNext(prevNode); prevNode = this

Reversing single linked list in C#

爷,独闯天下 提交于 2019-11-30 10:24:04
问题 I am trying to reverse a linked list. This is the code I have come up with: public static void Reverse(ref Node root) { Node tmp = root; Node nroot = null; Node prev = null; while (tmp != null) { //Make a new node and copy tmp nroot = new Node(); nroot.data = tmp.data; nroot.next = prev; prev = nroot; tmp = tmp.next; } root = nroot; } It is working well. Was wondering if it possible to avoid creating new node. Would like to have suggestions on this. 回答1: Node p = root, n = null; while (p !=

strategies to reverse a linked list in JavaScript

孤街浪徒 提交于 2019-11-30 03:38:17
I just struggled through a simple interview question: Please reverse a singly linked list. While I failed to provide a working answer in time to save the interview, I was able to come up with a solution afterwards. Is my solution correct? How would you analyze this with Big-Oh? Are there more efficient ways to reverse a singly linked list? // reverse a linked list var reverseLinkedList = function(linkedlist) { var node = linkedlist; var previous = null; while(node) { // reverse pointer node.next = previous; // increment previous to current node previous = node; // increment node to next node