Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

linked list sorting in java

// Java program to sort a Linked List using Bubble Sort
 
public class SortList {
 
    // Represent a node of the singly linked list
    class Node {
        int data;
        Node next;
 
        public Node(int data)
        {
            this.data = data;
            this.next = null;
        }
    }
 
    // Represent the head and tail of the singly linked list
    public Node head = null;
    public Node tail = null;
 
    // addNode() will add a new node to the list
    public void addNode(int data)
    {
 
        // Create a new node
        Node newNode = new Node(data);
 
        // Checks if the list is empty
        if (head == null) {
 
            // If list is empty, both head and tail will
            // point to new node
            head = newNode;
            tail = newNode;
        }
        else {
 
            // newNode will be added after tail such that
            // tail's next will point to newNode
            tail.next = newNode;
 
            // newNode will become new tail of the list
            tail = newNode;
        }
    }
 
    // sortList() will sort nodes of the list in ascending
    // order
    public void sortList()
    {
 
        // Node current will point to head
        Node current = head, index = null;
 
        int temp;
 
        if (head == null) {
            return;
        }
        else {
            while (current != null) {
                // Node index will point to node next to
                // current
                index = current.next;
 
                while (index != null) {
                    // If current node's data is greater
                    // than index's node data, swap the data
                    // between them
                    if (current.data > index.data) {
                        temp = current.data;
                        current.data = index.data;
                        index.data = temp;
                    }
 
                    index = index.next;
                }
                current = current.next;
            }
        }
    }
 
    // display() will display all the nodes present in the
    // list
    public void display()
    {
        // Node current will point to head
        Node current = head;
 
        if (head == null) {
            System.out.println("List is empty");
            return;
        }
        while (current != null) {
            // Prints each node by incrementing pointer
            System.out.print(current.data + " ");
            current = current.next;
        }
 
        System.out.println();
    }
 
    public static void main(String[] args)
    {
 
        SortList sList = new SortList();
 
        // Adds data to the list
        sList.addNode(8);
        sList.addNode(3);
        sList.addNode(7);
        sList.addNode(4);
 
        // Displaying original list
        System.out.println("Original list: ");
        sList.display();
 
        // Sorting list
        sList.sortList();
 
        // Displaying sorted list
        System.out.println("Sorted list: ");
        sList.display();
    }
}
Comment

how to sort linked list in java

//
// Sorts a list of strings alphabetically
// Comparator in sorted() can be replaced based on the type of your list
//

List<String> names = List.of("Nikos", "Sofia", "Klelia", "George");
// [Nikos, Sofia, Klelia, George]

List<String> sortedList = names.stream()
                .sorted((o1, o2) -> Collator.getInstance().compare(o1, o2))
                .collect(Collectors.toList());
// [George, Klelia, Nikos, Sofia]
Comment

PREVIOUS NEXT
Code Example
Java :: Java TestNG Data Provider example 
Java :: java mahout get clusters centers 
Java :: naming convention in selenium 
Java :: run java class file 
Java :: Java How to use NavigableMap? 
Java :: How do you make bedrock full screen in Minecraft? 
Java :: how to Compile the source code in ./src folder with libraries in ./lib folder using JavaSE-1.7 
Java :: java stream dristinct 
Java :: program Pr115_3; var k, n : integer; suma : real; begin readln(n); suma := 0; for k := 1 to n do suma := suma + 1 / sqr(2*k+1); writeln(suma); readln; end. 
Java :: search and delete class files from jars 
Java :: java and python begineers mcq with answers 
Java :: connect as SYSDBA java 
Java :: access char in string 
Java :: how to make a messages config minecraft plugin 
Java :: java applet draw house 
Java :: var keyword not working in spring boot application 
Java :: no main attribute in java android 
Java :: Java Catching base exception class only 
Java :: image show by timer android studio 
Java :: how to add a singleton hashset in java 
Java :: num1 * num2 
Java :: javafx open alert window 
Java :: Kotlin const to Java 
Java :: Java @SuppressWarnings Annotation Example 
Java :: java code to implement hybrid interface 
Java :: priority queue java remove 
Java :: get whatsapp group id flutter 
Java :: log errors with stack traces spring 
Java :: Java Change values of variables 
Java :: print out list of letters a to z java 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =