Question

In: Computer Science

Write java code to reverse a linked list. Fill in reverseLists() ReverseLinkedList.java package mid; public class...

Write java code to reverse a linked list. Fill in reverseLists()



ReverseLinkedList.java

package mid;

public class ReverseLinkedList {

    private static class ListNode {

        int val;

        ListNode next;

        ListNode() {

        }

        ListNode(int val) {

            this.val = val;

        }

        ListNode(int val, ListNode next) {

            this.val = val;

            this.next = next;

        }

    }

    public static void printList(ListNode l1) {

        ListNode cur = l1;

        while(cur != null) {

            System.out.println(cur.val);

            cur = cur.next;

        }

    }

    public static ListNode reverseLists(ListNode h1) {

    }

    public static void main(String[] args) {

        ListNode l1_4 = new ListNode(4);

        ListNode l1_3 = new ListNode(3, l1_4);

        ListNode l1_2 = new ListNode(2, l1_3);

        ListNode l1_1 = new ListNode(1, l1_2);

        ListNode h1 = l1_1;

        System.out.println("list is:");

        printList(h1);

        ListNode h2 = reverseLists(h1);

        System.out.println("Reversed list is:");

        printList(h2);

    }

}

Solutions

Expert Solution

public class Main {

    private static class ListNode {

        int val;

        ListNode next;

        ListNode() {

        }

        ListNode(int val) {

            this.val = val;

        }

        ListNode(int val, ListNode next) {

            this.val = val;

            this.next = next;

        }

    }
//to diplay the list
    public static void printList(ListNode l1) {

        ListNode cur = l1;

        while(cur != null) {//iterating until null value

            System.out.println(cur.val);//printing node value

            cur = cur.next;

        }

    }
//-----------------------------------------------------------
//reversing the list by sending the header as parameter to it
    public static ListNode reverseLists(ListNode h1) {

    ListNode prev = null;
    ListNode current = h1;
        ListNode next = null;
        while (current != null) { //iterating until null
            //swaping the nodes in list
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        h1= prev;
        return h1; //returning the header
      
    }
//-------------------------------------------------------------------------
    public static void main(String[] args) {
    //CREATING LINKED LIST

        ListNode l1_4 = new ListNode(4);

        ListNode l1_3 = new ListNode(3, l1_4);

        ListNode l1_2 = new ListNode(2, l1_3);

        ListNode l1_1 = new ListNode(1, l1_2);

        ListNode h1 = l1_1;

        System.out.println("list is:");
  
        printList(h1);

       ListNode h2 = reverseLists(h1);

        System.out.println("Reversed list is:");

        printList(h2);

    }

}

. Fill in reverseLists()


Related Solutions

Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
Objective: Manipulate the Linked List Pointer. Write a java subclass to extend LList.java. Provide a reverse...
Objective: Manipulate the Linked List Pointer. Write a java subclass to extend LList.java. Provide a reverse list method in the subclass to reverse the order of the linked list. Print the original linked list and the reverse ordered linked list at the end of program. You can use the gamescore.txt to test the reverse method. _____________________________________________________________________________________________________________________________________________________ /** Source code example for "A Practical Introduction to Data     Structures and Algorithm Analysis, 3rd Edition (Java)"     by Clifford A. Shaffer     Copyright 2008-2011 by...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private E[] queue;    private int front = 0, rear = 0;    private static final int DEFAULT_CAPACITY = 5;       public CircularQueue(int capacity)    {    queue = (E[]) new Object[capacity + 1];    }       public CircularQueue()    {        this(DEFAULT_CAPACITY); }       //Add a method that will determine if the queue is empty. Recall that the queue is...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT