Question

In: Computer Science

What will be the final linked-list after executing the following method on the given input singly...

What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference.

Input: 1->2->3->4->5->6->7->8->null                                                   

void method(list){

if(list.head == null) return;

Node slow_ref = list.head;

Node fast_ref = list.head;

Node prevS = null;

Node prevF = null;

while(fast_ref != null && fast_ref.next != null){

prevS = slow_ref;

slow_ref = slow_ref.next;

prevF = fast_ref;

fast_ref = fast_ref.next.next;

}

prevS.next = slow_ref.next;

prevF.next.next = slow_ref;

slow_ref.next = null;

}

Solutions

Expert Solution

Here we are re-adjusting the pointers to modify the linked list.

slow_ref is 5 and prevS is 4

fast_ref is null and prevF is 7

After adjusting the links, linked list is

1->2->3->4->6->7->8->5->NULL

i.e slow pointer is moved at the end of the linked list

Program Execution

class LinkedList{
    Node head;
    void add(int i){
        Node newNode= new Node(i);
        if(head==null){
            head = newNode;
            return;
        }
        Node temp = head;
        while (temp.next!=null){
            temp = temp.next;
        }
        temp.next = newNode;
    }

    void print(){
        Node tem = head;
        while(tem!=null){
            System.out.print(tem.item+"->");
            tem = tem.next;
        }
        System.out.println("NULL");
    }
}
class Main{

    static void method(LinkedList list){
        if(list.head == null) return;
        Node slow_ref = list.head;
        Node fast_ref = list.head;
        Node prevS = null;
        Node prevF = null;
        while(fast_ref != null && fast_ref.next != null){
            prevS = slow_ref;
            slow_ref = slow_ref.next;
            prevF = fast_ref;
            fast_ref = fast_ref.next.next;
        }

        prevS.next = slow_ref.next;
        prevF.next.next = slow_ref;
        slow_ref.next = null;
    }


    public static void main(String[] args) {
        LinkedList ll = new LinkedList();
        for(int i=1;i<=8;i++){
            ll.add(i);
        }
        ll.print();
        method(ll);
        ll.print();
    }
}

Output


Related Solutions

Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Create a ValueGet() method that takes a linked list as input and an integer index and...
Create a ValueGet() method that takes a linked list as input and an integer index and returns the value stored in the node at that index position. Sample Input: 01->100->300->214, index = 2 Output: 300 At index 2 the node has a value of 300 give me the full code. Give the full code with c++
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT