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 :: input array through scanner in java 
Java :: how to test how many of one character is in a string java 
Java :: load a file from classpath spring boot 
Java :: gravity in java 
Java :: how to change orientation of screen in android in fragmetn programmatically 
Java :: java shorthand if 
Java :: first and last digit sum java 
Java :: java get current date 
Java :: groovy string to json 
Java :: arraylist string to string array 
Java :: java current time millis 
Java :: Java Using forName() method 
Java :: how to get system date in android 
Java :: java array loop backwards 
Java :: how to create a random number in java 
Java :: date java format 
Java :: hashmap sort by value descending 
Java :: java print output 
Java :: console printing in java 
Java :: how to add minutes in date in java 
Java :: how to iterate hashmap java 
Java :: list to array java 
Java :: prime number between given range in java 
Java :: random word java 
Java :: conditions in for loop java 
Java :: @data lombok 
Java :: java get variable from another class 
Java :: hashmap foreach 
Java :: get username from jwt token request spring boot 
Java :: android sharedpreferences 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =