Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Sorting LinkedList

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Sorting LinkedList

    I'm working on a program where I have an issue sorting my list. (I just started with Linked List)
    Can you help me with how to sort the list from the code I just wrote? Or suggest me a code for it?


    Here is my code :
    //124. I. Write a Java program to find the index of a value in a sorted array. II.If the value does not find return the index where it would be if it were inserted in order.
    // My approach is to convert the array to a list. For case II, I planned to add the value to the list, sort it and then get the index. 
     
    import java.util.*;
     
    public class indexSortedArray {
        public static void main(String[] args) {
     
            Integer[] nums = { 1, 2, 4, 5, 6 };
     
            List<Integer> list = Arrays.asList(nums);
     
            java.util.LinkedList<Integer> linkedList = new java.util.LinkedList<Integer>(list);
            linkedList.add(0);
     
     
            System.out.println("LinkedList of above array : "
                    + linkedList);
     
            // int getIndex = Arrays.asList(nums).indexOf(0);
            // System.out.println("0 is located at " + getIndex + " index");
     
            // if (getIndex == -1) {
            // Arrays.asList(nums).add(0);
            // }
        }
     
    }

    I tried several approaches but it didn't work. My last approach was with the following piece of code I added but didn't do work so I removed it.
    public Node head = null;
     
        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;
                }
            }
        }

  2. #2
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Sorting LinkedList

    sortList() is working,
    might something in rest of LinkedList can be wrong
    (I suppose you wrote your own LinkedList class)
    Last edited by zemiak; May 18th, 2022 at 03:51 AM.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Sorting LinkedList

    I changed my code and used Collections.sort()

    Way more easier to use to sort list

Similar Threads

  1. [SOLVED] linkedlist concurrentmodificationexception
    By dEvilKinG in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2013, 12:20 PM
  2. cant get LinkedList to compile
    By chuy525 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 1st, 2012, 08:32 PM
  3. is a Set the same as a Stack or LinkedList
    By mia_tech in forum Java Theory & Questions
    Replies: 1
    Last Post: July 9th, 2012, 05:50 PM
  4. [SOLVED] Sorting LinkedList
    By wdh in forum What's Wrong With My Code?
    Replies: 28
    Last Post: April 29th, 2012, 12:52 PM
  5. LinkedList Iterator
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2011, 09:51 PM

Tags for this Thread