Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 749529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:26:43+00:00 2026-05-14T14:26:43+00:00

I have a simple linked list. The node contains a string (value) and an

  • 0

I have a simple linked list. The node contains a string (value) and an int (count).

In the linkedlist when I insert I need to insert the new Node in alphabetical order. If there is a node with the same value in the list, then I simply increment the count of the node.

I think I got my method really screwed up.

 public void addToList(Node node){
        //check if list is empty, if so insert at head
        if(count == 0 ){
            head = node;
            head.setNext(null);
            count++;
        }
        else{
            Node temp = head;
            for(int i=0; i<count; i++){
                //if value is greater, insert after
                if(node.getItem().getValue().compareTo(temp.getItem().getValue()) > 0){
                    node.setNext(temp.getNext());
                    temp.setNext(node);                   
                }
                //if value is equal just increment the counter
                else if(node.getItem().getValue().compareTo(temp.getItem().getValue()) == 0){
                    temp.getItem().setCount(temp.getItem().getCount() + 1);
                }
                //else insert before
                else{
                    node.setNext(temp);
                }
            }
        }      

    }

Ok so this is inserting all my strings, but not in alphabetical order. Do you see any error?

 public Node findIsertionPoint(Node head, Node node){
        if( head == null)
            return null;

        Node curr = head;
        while( curr != null){
            if( curr.getValue().compareTo(node.getValue()) == 0)
                return curr;
            else if( curr.getNext() == null || curr.getNext().getValue().compareTo(node.getValue()) > 0)
                return curr;
            else
                curr = curr.getNext();
        }

        return null;
    }

    public void insert(Node node){
        Node newNode = node;
        Node insertPoint = this.findIsertionPoint(this.head, node);
        if( insertPoint == null)
            this.head = newNode;
        else{
            if( insertPoint.getValue().compareTo(node.getValue()) == 0)
                insertPoint.getItem().incrementCount();
            else{
                newNode.setNext(insertPoint.getNext());
                insertPoint.setNext(newNode);
            }
        }
        count++;
    }
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:26:44+00:00Added an answer on May 14, 2026 at 2:26 pm

    There are a few bugs with your code:

    • Inserting at/before head actually needs to happen in two different scenarios:
      • If the list is empty, head becomes node
      • If the list is not empty, but node is less than the first element, head also becomes node
        • In either case, node links to whatever head was pointing to before (null or a real node), and head now points to node.
    • If you’re not inserting before head, then you must be inserting after some node. We just need to find where this place is. There are two scenarios:
      • node.getValue() > temp.getValue(), and node.getValue() < temp.getNext().getValue()
      • node.getValue() > temp.getValue() and temp.getNext() == null
        • In either case, node is inserted between temp and temp.getNext()

    I suggest encapsulating the after insertion point search in its own function. That is, given the list and a value, it needs to return a node. If that node has the same value as the search value, then simply increment; otherwise, insert after. As a special case, return null to indicate that the insertion point is before head.


    In pseudocode, it’ll look like this:

    FUNCTION findInsertionPoint(Node head, V value) RETURNS Node
      // return null if value needs to be inserted before head
      IF head == null OR value < head.getValue()
         RETURN null;
    
      // otherwise, either return a node with the given value,
      // or return a node after which value should be inserted
      Node curr = head;
      REPEAT
         IF curr.value == value
            RETURN curr;
         ELSEIF curr.getNext() == null OR curr.getNext().getValue() > value
            RETURN curr;
         ELSE
            curr = curr.getNext();
    
    PROCEDURE insert(V value) {
      Node newNode = NEW Node(value);
      Node insertPoint = findInsertionPoint(this.head, value);
      IF insertPoint == null // insert before head
         newNode.setNext(this.head);
         this.head = newNode;
      ELSE
         IF insertPoint.getValue() == value
            insertPoint.incrementCounter();
         ELSE // insert after insertPoint
            newNode.setNext(insertPoint.getNext());
            insertPoint.setNext(newNode);
    

    Update: I see that you’ve translated my pseudocode to Java, but for some reason you’ve omitted codes that deals with inserting before head when head is not empty. Specifically, you have inexplicably omitted this part:

    IF head == null OR value < head.getValue()
                 // ^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    and this part:

    IF insertPoint == null 
       newNode.setNext(this.head); // <<<<<<<<<<<
       this.head = newNode;
    

    Both of these are essential; it’s what allows "A" to be inserted before the head in [ "B", "C", "D" ].

    You need to understand why they’re important, and really ask yourself why you chose to remove them. Explain to us, to me, to yourself, why you did that; realize the mistake and learn from it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found WINK very good. However, you can find other,… May 14, 2026 at 7:39 pm
  • Editorial Team
    Editorial Team added an answer I suppose it is inefficient because the handler is placed… May 14, 2026 at 7:39 pm
  • Editorial Team
    Editorial Team added an answer "How big of a jump will it be to go… May 14, 2026 at 7:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.