Lists Comments
Comments
-
import java.util.Scanner;
class LinkedList {
Node head; Node tail; /* * Adds Node to end of list with given `num` as data
* Make sure it correctly sets the head (and tail) * when adding the first item to the list */ public void add(int num){ // your code here this.add(num); } /* * Returns value of node at given index */ public int get(int index){ // your code here return this.get(index); }
}
class Node{
int data;
Node next;
public Node(int d){
data = d;
}public Node(int d, Node n){ data = d; next = n; }
}
public class Main {
public static void main(String[] args) { LinkedList list = new LinkedList(); Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int b= in.nextInt(); if(a == -6){ System.out.println(list.get(b)); } else if(a == -9){ list.add(b); } } }
}
I don't understand this exercise
-
public void add(int num){ head = tail; tail.next = new Node(num); tail = tail.next; } /* * Returns value of node at given index
*/ public int get(int index){ temp = head; for (int i = 0; i<index; i++){ temp = temp.next; } return; }
}
-
what am i supposed to return?
-
import java.util.Scanner;
class LinkedList {
Node head; Node tail; private Node temp = null; /* * Adds Node to end of list with given `num` as data
* Make sure it correctly sets the head (and tail) * when adding the first item to the list */ public void add(int num){ head = tail; tail.next = new Node(num); tail = tail.next; } /* * Returns value of node at given index */ public int get(int index){ temp = head; for (int i = 0; i<index; i++){ temp = temp.next; } return temp; }
}
class Node{
int data;
Node next;
public Node(int d){
data = d;
}public Node(int d, Node n){ data = d; next = n; }
}
public class Main {
public static void main(String[] args) { LinkedList list = new LinkedList(); Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int b= in.nextInt(); if(a == -6){ System.out.println(list.get(b)); } else if(a == -9){ list.add(b); } } }
}
-
this is telling me that my return value needs to be an integer, I'm lost
-
@sarah, you can link to long pieces of code so you don't need to paste them here. The method
get
needs to return an integer (the value or data of a node). See if the new hint helps (or the new cheat option if you need it.) -
thanks, i realize what i was doing wrong.
-
Do i understand it correct if i say that head and tails arent nodes themselves?
-
No, the first node in the list is the head and the last node is the tail. (If there's just one node, it's both the head and tail.)
-
When i state tail is head , can i consider them as one node with 2 names (startingpoint), or are they two different nodes with the same name. In case two i dont understand the line:
tail.next = node ( i guess this is the connection to the new node)
tail=node ( this is strange, since now tail is my new nood, but the connection is gone, or is in head of??I dont really get this as you might of guessed while reading my question
-
class LinkedList {
Node head;
Node tail;So i have two 'dummy' nodes. But what is the data en what is the pointer of head.
if head==0 -------------> what does this mean, what is null, the pointer? Then what is the data?
-
Nevermind, i understand it now. Its cause the default points to null.
Also i understand why i can say tail.next and it does the same for the head. This cause its a reference variable. So i guess i figured it out :-)
-
Why is it supposed to be
head = tail...
and not
head.next=tail;THis is driving me nuts! I drew this out on paper TWICE and while I understand the concept... I cant code it!
-
Which part are you referring to? When there's only one item in the list, that item is both the head and the tail. This a correct solution.
-
is that python 2.6
I am using python 3.5 working ok on my ide but here it is failing on
reading data. -
Yes it's python 2.
Ruchika
Nov 20, 7:50 PMList created by add operation as said in the above post is 3 5 4 7 , I think the list will be 3 5 7 4 and not 3 5 4 7. Please correct me if my understanding is wrong