Question: //new to linkedlists how would you add remove first and last element to this code: //Tips are appreciated about how to make linkedlist functions from

//new to linkedlists how would you add remove first and last element to this code:

//Tips are appreciated about how to make linkedlist functions from fresh

//Thanks

import java.util.Scanner;

public class ListNode { public int item; public ListNode next; public static ListNode head; public static Scanner scanner; //made this for easier access public static String easyAnswer; public ListNode(int item, ListNode next) { this.item= item; this.next= next; }

public ListNode(int item) { this(item, null); }

public static void NodeRemoveFirst() { //removing first node head= new ListNode(); //confused if i need to construct a new head here .. if(head==null) { return; } //ListNode current= head; { head= head.next; } } public static void NodeRemoveLast() { //removing last node } @Override public String toString() //remember we want all the elements of list print but null {//making method to print our linkedlist if(next != null) { return item + "\t" + next; } else { return item+ "\t"; } } public static void main(String[] args) { ListNode list= new ListNode(11, new ListNode(12, new ListNode(15, new ListNode(11, null)))); System.out.println(list); System.out.println("If you would like to remove first node from list type 'first' If you would like to remove last node from list type 'last' "); scanner = new Scanner(System.in); easyAnswer = scanner.nextLine(); if (easyAnswer.equalsIgnoreCase("first")) { NodeRemoveFirst(); } else if (easyAnswer.equalsIgnoreCase("last")) { NodeRemoveLast(); } System.out.println("This is that last state of your list"); System.out.println(list); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!