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

  • SEARCH
  • Home
  • 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 8095363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:06:14+00:00 2026-06-05T21:06:14+00:00

New programmer here, I am trying to understand and break down this code below

  • 0

New programmer here, I am trying to understand and break down this code below for a remove method, sorted linked list. I have added comments below for what my understand is and what i do not understand. can someone shed some light on the things which are unclear?

thanks in advance.

/* 1  */ public void remove(E e) throws NotFoundException{
/* 2  */     Node<E> p; //declares node p
/* 3  */     // chunk below determines where to start traversing based on element value. should traverse from head if new element < pos value
/* 4  */     if(pos == head || pos.compareTo(e) >= 0 ){ //I do not understand 2nd equality..why?
/* 5  */         p = head; //traverse list from head
/* 6  */     }else{
/* 7  */         //traverse list from pos
/* 8  */         p = pos;
/* 9  */     }
/* 10 */     for( ;p.next!=null && p.next.compareTo(e)<0; p = p.next); //nothing to initialize?
/* 11 */     //e not found in the list
/* 12 */     if(p.next == null || p.next.compareTo(e) > 0){
/* 13 */         throw new NotFoundException();
/* 14 */     }
/* 15 */     if(p.next == pos){
/* 16 */         //if node to be deleted is pos, update pos to head
/* 17 */         pos = head;
/* 18 */     }
/* 19 */     p.next = p.next.next; //delete node
/* 20 */ }
  • 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-05T21:06:16+00:00Added an answer on June 5, 2026 at 9:06 pm
    4. if(pos == head || pos.compareTo(e) >= 0 ){ //I do not understand 2nd equality..why? 
    5. p = head; //traverse list from head
    6. }else{
    7. //traverse list from pos 
    8. p = pos;  
    9. }
    

    First, here is a documentation for compareTo
    The 2nd equality checks if pos points to a node that comes after “e”. if it’s true then u must traverse the list from its head because e comes before pos. Else, e comes after pos so you can traverse the list from pos. This is true because the list is sorted.

    10. for( ;p.next!=null && p.next.compareTo(e)<0; p = p.next); //nothing to initialize?
    11. //e not found in the list
    12. if(p.next == null || p.next.compareTo(e) > 0){
    13. throw new NotFoundException();
    14. }
    

    Here you start scanning the list from the position that was chosen and if you get to a node that is null (the end of the list) or a node that is greater than “e” then you know that “e” is not found in the list (because the list is sorted), so you throw an exception

    Line 10: you don’t have to initialize anything here because you already initialized p above.

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

Sidebar

Related Questions

relatively new programmer here. Look at the two classes defined below. I'd like to
New Programmer here. Trying space invaders. I have a 2 dimensional array of objects
New programmer here with only some minor Java experience trying my hand at writing
New Android programmer here. I have a Service which performs socket management and async
Let me preface by stating I'm a new programmer - an IT guy trying
I am new programmer in Obj-C and cocoa . Im a trying to write
I am a relatively new programmer so this may be a really simple question
In trying to understand the correlation between program objects and data in tables (here:
Novice programmer here. trying to learn static methods and recursion. No idea why I
Entry level programmer here trying to implement a COM interface. I am working on

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.