Question

class Node { public Object data = null; public Node next = null; Node head = new Node(); // first Node head.next = new Node()

what one line of code would i need to remove the first node?

0 0
Add a comment Improve this question Transcribed image text
Answer #0

//one line code to remove first node would be

head=head.next;

=======================

//I am giving complete program to understand it better

//Let's have Node.java file in which class Node is defined and in Main.java we are going to decare list of type Node to represent linked list

Node.java

class Node

{

  public Object data=null;

  public Node next=null;

}

=====================

//Main.java

class Main {

public static void main(String[] args) {

Node head=new Node(); //first node

//just adding data to show how it works

head.data=1;

head.next=new Node(); //second node

head.next.data=2;

head.next.next=new Node(); //third node

head.next.next.data=3;

head.next.next.next =new Node(); //fourth node

head.next.next.next.data=4;

//print data before removing first node

System.out.println("Linked list elements are : "+head.data+" "+head.next.data+" "+head.next.next.data+" "+head.next.next.next.data);

//one line code to remove first node would be

head=head.next;

System.out.println("Linked list after removing first node is : "+head.data+" "+head.next.data+" "+head.next.next.data);

}

}

/*Output

 java -classpath .:/run_dir/junit-4.12.jar Main

Linked list elements are : 1 2 3 4

Linked list after removing first node is : 2 3 4

*/

As first node with data 1 is removed after statement

head=head.next;

we get linked list as 2 3 4

Add a comment
Know the answer?
Add Answer to:
what one line of code would i need to remove the first node? class Node {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given the following partial code, fill in the blank to complete the code necessary to remove...

    Given the following partial code, fill in the blank to complete the code necessary to remove the second node. (don't forget the semicolon) class Node { public Object data = null; public Node next = null; Node head = new Node(); // first Node head.next = new Node(); // second Node head.next.next = new Node(); // third node head.next.next.next = new Node(); // fourth node head=head.next.next= new Node():

  • just a normal linked list Given the following partial code, fill in the blank to complete...

    just a normal linked list Given the following partial code, fill in the blank to complete last node. (don't forget the semicolon) class Node { public Object data = null; public Node next = null; Node head = new Node(); // first Node head.next = new Node; // second Node head.next.next = new Node: // third node head.next next next = new Node: // fourth node

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • Hey I really need some help asap!!!! I have to take the node class that is...

    Hey I really need some help asap!!!! I have to take the node class that is in my ziplist file class and give it it's own file. My project has to have 4 file classes but have 3. The Node class is currently in the ziplist file. I need it to be in it's own file, but I am stuck on how to do this. I also need a uml diagram lab report explaining how I tested my code input...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Consider java for fixing this code please: what i need is to insert method to be...

    Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • Consider the following Java program public class Foo i static class Node Object item; Node next:...

    Consider the following Java program public class Foo i static class Node Object item; Node next: Node (Object item, Node next) this. item = item; this.next next: public static void main (String] args) ( Node dat a nu ï ï ; dat a = new Node ("hello", data): data ne Node (5, data) while (data null) String s(String) data. item; System.out.println (s)

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT