Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

import java.io.*;
import java.util.stream.*;

public class Solution {

static class ListCell<T> {

public T datum; // Data for this cell
public ListCell<T> next; // Next cell

public ListCell(T datum, ListCell<T> next) {
this.datum = datum;
this.next = next;
}
}

static class LinkedList<T> {

private static final String STRING = " ";

Solution.ListCell<T> head; // head (first cell) of the List

public LinkedList() {
head = null;
}

public void insert(T element) {
head = new ListCell<T>(element, head);
}

public void delete(T element) {
delete(element, head);
}

private ListCell<T> delete(T element, ListCell<T> cell) {
if (cell == null)
return null;
if (cell.datum.equals(element))
return cell.next;
cell.next = delete(element, cell.next);
return cell;
}

public int size() {
return size(head);
}

private int size(ListCell<T> cell) {
if (cell == null)
return 0;
return size(cell.next) + 1;
}

public String toString() {
return toString(head);
}

private String toString(ListCell<T> cell) {
if (cell == null)
return "";
return cell.datum.toString() + STRING + toString(cell.next);
}
}

// Complete the mergeSort function below.
// !!! Leave the code as is except for the below function, !!!
// !!! though writing helper function(s) are allowed. !!!
private static void sort(Solution.LinkedList<Integer> llist) {

}


public static void main(String[] args) throws IOException {

BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int llistCount = Integer.parseInt(br.readLine().trim());

LinkedList<Integer> llist = new LinkedList<>();

IntStream.range(0, llistCount).forEach(i -> {
try {
Integer llistItem = Integer.parseInt(br.readLine().trim());

llist.insert(llistItem);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});

sort(llist);

bufferedWriter.write(llist.toString().trim());

bufferedWriter.close();

br.close();
}

}

Transcribed Image Text:Merge sort would in general be the best sorting algorithm for linked lists due to the slow access to individual elements in a list, O(n). In this challenge, you need to implement the sort method, based on the linked list structure defined in the code stub. Expected time complexity is O(nlogn), space complexity is O(n). Input Format The input is handled by code editor: - The values for the list are space separated and given in the same line. Constraints • Values range from 0 to 2^31 - 1. • Number of nodes in the list ranges from 0 to 1,000,000. Output Format Space separated node values of the sorted linked list in a single line. Sample Input 0 2 4 Sample Output 0 2 2 4 5 6 Explanation 0
Transcribed Image Text:Space separated node values of the sorted linked list in a single line. Sample Input 0 5 2 4 6 2 Sample Output 0 2 2 4 5 6 Explanation 0 Sort the 5 values in ascendign order.
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Computer Networking: A Top-Down Approach (7th Edi...
    Computer Engineering
    ISBN:9780133594140
    Author:James Kurose, Keith Ross
    Publisher:PEARSON
    Text book image
    Computer Organization and Design MIPS Edition, Fi...
    Computer Engineering
    ISBN:9780124077263
    Author:David A. Patterson, John L. Hennessy
    Publisher:Elsevier Science
    Text book image
    Network+ Guide to Networks (MindTap Course List)
    Computer Engineering
    ISBN:9781337569330
    Author:Jill West, Tamara Dean, Jean Andrews
    Publisher:Cengage Learning
    Text book image
    Concepts of Database Management
    Computer Engineering
    ISBN:9781337093422
    Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
    Publisher:Cengage Learning
    Text book image
    Prelude to Programming
    Computer Engineering
    ISBN:9780133750423
    Author:VENIT, Stewart
    Publisher:Pearson Education
    Text book image
    Sc Business Data Communications and Networking, T...
    Computer Engineering
    ISBN:9781119368830
    Author:FITZGERALD
    Publisher:WILEY