import java.io.*; import java.util.stream.*; public class Solution { static class ListCell { public T datum; // Data for this cell public ListCell next; // Next cell public ListCell(T datum, ListCell next) { this.datum = datum; this.next = next; } } static class LinkedList { private static final String STRING = " "; Solution.ListCell head; // head (first cell) of the List public LinkedList() { head = null; } public void insert(T element) { head = new ListCell(element, head); } public void delete(T element) { delete(element, head); } private ListCell delete(T element, ListCell 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 cell) { if (cell == null) return 0; return size(cell.next) + 1; } public String toString() { return toString(head); } private String toString(ListCell 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 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 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(); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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();
}

}

 

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: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
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.
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY