Question

The idea of InsertSort is to sort the data by inserting a value to already sorted...

The idea of InsertSort is to sort the data by inserting a value to already sorted data. Our textbook introduced InsertSort for arraylist. Now please write code (psudeocode is fine) to implement InsertSort to sort data from small to large for a Linked LIst. Note that it is NOT doubly linked list. Your code should have the O(n) complexity in the best case

/**definition for singly linked list

* public class ListNode {

* int val;

* LIstNode next;

* LIstNode(int x) {val = x;}

* }

*/

public class Solution {

public ListNode insertionSortList(ListNode head) {

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public ListNode insertionSortList(ListNode head) {
       {
           ListNode sorted = null;
           ListNode current = head;
           while (current != null)
           {
               ListNode next = current.next;
               sortedInsert(current, sorted);
               current = next;
           }
           head = sorted;
           return head;
       }
   }

   private void sortedInsert(ListNode newnode, ListNode sorted)
   {
       if (sorted == null || sorted.val >= newnode.val)
       {
           newnode.next = sorted;
           sorted = newnode;
       }
       else
       {
           ListNode current = sorted;
           while (current.next != null && current.next.val < newnode.val)
           {
               current = current.next;
           }
           newnode.next = current.next;
           current.next = newnode;
       }
   }

Add a comment
Know the answer?
Add Answer to:
The idea of InsertSort is to sort the data by inserting a value to already sorted...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha *...

    Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha * Definition for singly-linked list. * public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. class Solution { public ListNode merge TwoLists(ListNode li, ListNode 12) { 10 Example: Input: 1->2->4, 1-3-4...

  • I need help completing this with C++ /** * Definition for singly-linked list. * struct ListNode...

    I need help completing this with C++ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) {    } };

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • if we have following List classes: public class LinkedList<T> { class ListNode { protected T value;...

    if we have following List classes: public class LinkedList<T> { class ListNode { protected T value; protected ListNode next; public ListNode(T val, ListNode nxt) { value = val; next = nxt; } public ListNode(T val) { this(val, null); } public ListNode() { this(null, null); } } can you write the folowing methods in java: 1.Write a method for the LinkedList class called int indexOf(T val) which returns the integer index indicating the location of val in the list, or -1...

  • Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario...

    Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario There are some classic data structures in computer science. One example of this that you've seen in the lectures is called the stack. In the next few exercises, we'll build yet another classic data structure, the linked list. To be exact, we'll be building the singly linked list. The building block of a singly linked list is a node, which consists of two parts:...

  • JAVA lists and nodes my answer was : ListNode newNode = new ListNode(3); newNode.next = head;...

    JAVA lists and nodes my answer was : ListNode newNode = new ListNode(3); newNode.next = head; head = newNode; it is incorrect. what could it be? linkedNodes 11 > <linkedNodes 9 Main Page -- Problems - Solve a Problem OBJP5 Self-Check 16.10: linkedNodes 10 Language/Type: Java ListNodes LinkedLists Related Links: LinkedIntList.java Author: Marty Stepp (on 2019/09/19) Write the code necessary to convert the following sequence of ListNode objects: list -> [1] -> [2] / Into this sequence of ListNode objects:...

  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT