singly-linked-list

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

前提是你 提交于 2019-12-19 11:05:26
问题 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

strategies to reverse a linked list in JavaScript

放肆的年华 提交于 2019-12-18 11:27:05
问题 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

Sorting a linked list in C

夙愿已清 提交于 2019-12-17 19:00:17
问题 I'm trying to sort a linked list by finding the largest value, deleting it from its position, and then inserting it at the top of the list. The difficulty I'm running into is the actual deleting and inserting at the top. The issue seems to be in the if condition in the while loop contained within the sortList function, but I'm not sure how to fix it. Any help would be appreciated. #include <stdio.h> #include <stdlib.h> typedef struct node{ int num; struct node *next; } Node, *NodePtr; void

Remove duplicates from an unsorted linked list

一世执手 提交于 2019-12-17 18:33:45
问题 import java.util.*; /* * Remove duplicates from an unsorted linked list */ public class LinkedListNode { public int data; public LinkedListNode next; public LinkedListNode(int data) { this.data = data; } } public class Task { public static void deleteDups(LinkedListNode head){ Hashtable<Integer, Boolean> table=new Hashtable<Integer, Boolean>(); LinkedListNode previous=null; //nth node is not null while(head!=null){ //have duplicate if(table.containsKey(head.data)){ //skip duplicate previous

Reverse Singly Linked List Java [duplicate]

限于喜欢 提交于 2019-12-17 07:06:02
问题 This question already has answers here : How to reverse a singly-linked list in blocks of some given size in O(n) time in place? (4 answers) Closed last year . Can someone tell me why my code dosent work? I want to reverse a single linked list in java: This is the method (that doesnt work correctly) public void reverseList(){ Node before = null; Node tmp = head; Node next = tmp.next; while(tmp != null){ if(next == null) return; tmp.next = before; before = tmp; tmp = next; next = next.next; }

Why can't I sort a user defined LinkedList with this java code?

余生颓废 提交于 2019-12-14 03:36:54
问题 I created a program in JAVA to add elements to a LinkedList and sort the elements while adding them. The swap technique I'm using in case of sorting is similar to that used while adding a node to the beginning of the LinkedList. The technique works in the latter case but fails to run in the former. I don't understand why this is not working. Below is my code for your reference. //Node class class Node{ int d; Node link; Node(int d){ this.d = d; link = null; } }//Node //LinkedList class class

Singly linked list node not able to be cast as Integer

我的未来我决定 提交于 2019-12-14 03:24:47
问题 I am writing a generic singly linked list as a homework assignment. I have been given the JUnit test that the instructor is going to use to test the code. When I run the test, gives me this error: java.lang.ClassCastException: SinglyLinkedList$Node cannot be cast to java.lang.Integer. If I am using a generic as the type, why can't I cast it as an Integer? Also, when I first ran the test, the bash showed: JUnit version 4.12 .E.E.E. And it stayed like this until I ended the process. I think

Never ending loop in displaying LinkedList in C++

試著忘記壹切 提交于 2019-12-13 06:43:17
问题 In the following code when i try to display the list after removing the first element, its resulting in endless loop of next elements. This problem doesn't occur when other than first element is removed. I have no clue why is this happening? Can someone please tell me where is the mistake? #include<iostream> using namespace std; class node { public: int data; node *link; }; class linkedlist { node *head; public: linkedlist() { head=NULL; } int add(int data1) { node *insertnode=new node;

How to add elements to the end of a linked list

僤鯓⒐⒋嵵緔 提交于 2019-12-13 06:14:25
问题 So I am trying to create a function in C++ that should add an element at the end of an linked list. This function that adds the elements should be called within the main -method. It should be possible to call it even if there is no element in the list. What I have so far is the following: int main() { ListElement* l = new ListElement; l->digit = 9; l->next = NULL; appendList(l, 5); appendList(l, 7); printList(l); return 0; } void appendList(ListElement *&l, int newDigit) { ListElement *lh =

Drawing consisting of two linked lists with pointers and values

China☆狼群 提交于 2019-12-13 03:54:59
问题 I just started taking an algorithms course, however, due to some family issues, I did not have a chance to participate in the first two lectures. Now I'm in a bit of a pickle, because I don't understand much of what is going on. Above is a picture of a task that I need to solve. From what i understand, L0 is a list containing all values of S and L1 is a list containing all values of S and a pointer to the corresponding value in L0. However, what I do not understand is when they start bringing