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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:50:25+00:00 2026-06-13T09:50:25+00:00

I am trying to write a peek method for a header linked list class.

  • 0

I am trying to write a peek method for a header linked list class. However, it doesn’t return the first value.

public E peek () throws NoSuchElementException {
  ListNode<E> temp = highest;
  ListNode<E> r = temp;

  if (temp.next == null) throw new NoSuchElementException();
  else r.next = temp.next.next;

  return r.next.value;
}

I understand why it doesn’t return the first value. Because in my code else r.next will already point to the next node in the list. So for 5,4,3,2,1 it will return 4 on the first call instead of 5. temp is pointing to the highest node which is the header node. How can I get the method to return the first value in the list, 5, first?

  • 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-13T09:50:26+00:00Added an answer on June 13, 2026 at 9:50 am

    A good way to implement Linked list is that header should always be empty node in the list so it should not hold a value. This way when you call next on header you actually go to first element only.

    header http://easy2teach.net/wp-content/uploads/2011/06/header-linked-list.jpg

    As depicted in above digram as header next is actually the first element of the Linked List

    So you Peek operation is not supposed to throw NoSuchElementException instead it should return null so simple method can be

     public E peek ()
     {
       if(check element does exist using size ==0)
          return null;
       else 
          return highest.next.value;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to write a procedure that given a value and a list, it deletes
I've had problem when trying write a function which has a default value when
Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void
Trying to write this small program to help me in my Stats class, everything
I'm trying write a program that takes a list of names, sorts those names,
We've been trying write unit tests for a worker class written in C#, which
I'm trying write a code that can the set property value through the lambda
I am trying write an application that receives lat/lng and return appropriate location. (i.e
I'm trying write a test for a method that returns a java.sql.Connection to connect
I am trying write a script where when a user clicks on a link,

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.