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 9305913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:02:39+00:00 2026-06-19T00:02:39+00:00

I’m looking for some advice on a Java assignment. What I’m asked to do

  • 0

I’m looking for some advice on a Java assignment. What I’m asked to do is perform different operations on numbers that are stored in a linked list, with each digit in a separate node. The point is to write a program that can do arithmetic on numbers that are very very large.

The particular problem that I’m looking for help on is for writing a method that compares two numbers, similar to regular compareTo() method for ints. It should return -1 if this.num < num, +1 if this.num > num, and 0 if they are equal.

What’s making this difficult for me is the fact that the assignment specifies that the linked lists should store the numbers in reverse order. For example, the linked list for the number 145 would look like:

5 => 4 => 1 => null

This makes it easier to add numbers together but it’s making it a headache for me when trying to compare. Here’s what I’ve come up with, the comments explain how it’s supposed to work.

public int compareTo(LLNum list)
{ // Compares two numbers.
  // If the two numbers are of a different length, clearly the shortest is the smallest.
  // If the two numbers are of equal length, call traverseToCompare to do the comparison.
  if (this.len > list.len)
  {
    compareResult = 1;
  }
  else if (this.len < list.len)
  {
    compareResult = -1;
  }
  else if (this.len == list.len)
  {
    traverseToCompare(this.head, list.head);
  }

  return compareResult;
}


public void traverseToCompare(ListNode node1, ListNode node2)
{ // In the case that both numbers are of the same length, recursively traverse the list.
  // compare each digit individually while unwinding the stack.
  // Once two digits are found to be different, break out of the unwinding (Note: I could not find a way of breaking out)
  // Since the dominant digit is at the tail end, this ensures the least number of   comparisons.
  if (node1 == null || node2 == null) 
  { // Base case. Handles the case where both numbers are identical.
    compareResult = 0;
    return;
  }
  traverseToCompare(node1.getNext(), node2.getNext());
  if (node1.getItem() > node2.getItem())
  {
    compareResult = 1;
  }
  if (node1.getItem() < node2.getItem())
  {
    compareResult = -1;
  }

  return; 
}

The numbers being in reverse order is what pulled me towards recursion. I thought I would recursively traverse the list and then compare each digit on the way out, and somehow break out of the recursion at the first digits that are not the same. I realize this is not a usual way to use recursion but I wasn’t sure how else to do it. Is there a way I could break without just throwing an exception? I think that might be a little too sloppy. Or any suggestions on how to do this without recursion?

Please don’t just give me some code to copy and paste. I’m just looking to be pointed in the right direction. Thanks!

  • 1 1 Answer
  • 0 Views
  • 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-06-19T00:02:40+00:00Added an answer on June 19, 2026 at 12:02 am

    If I had to do this I would first check the lengths of both lists (like you did). If they’re equal, a better way to do the comparison would be to create an iterator for each list. You can then increment the iterators at the same time and compare the values at that position in the linked lists. Doing it this way, you can simply stop comparing the lists once you have determined that one contains a larger number than the other.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.