-2
public class Node {
   ...
   public Node getNext() {...}         // return next field
   public int getData() {...}         // returns data
}

Assuming that the variable head points to (i.e. contains the address of) the first node of the linked list, write the statement(s) to print the data value in every other node of the linked list to the console. For example if the list has 5->4->3->2->1, the output should be 5 3 1 i.e. the numbers only with spaces in-between. If the list is empty 0 size), you code should not print out anything.

You may declare additional variables and assume that the list may have any number of nodes including zero. If the list is empty, do not print anything, otherwise print the data separated by white spaces.

What i tried:

 if (head == null) {
                        break;
                    }
                else {
                    while (head != null) {
                        int current = head.getData();
                        System.out.print(current + " ");
                        head = head.getNext();
                        if (head == null) {
                            break;
                        }
                        head = head.getNext();
                }
            }
  • Does this answer your question? [Calculating average of an array list?](https://stackoverflow.com/questions/10791568/calculating-average-of-an-array-list) – António Ribeiro Apr 17 '20 at 10:30

2 Answers2

2

Create a Node variable that points to the head of the linked list. You can then manipulate the variable and pass through every second node, until the variable is null, meaning that you have traversed the entire linked list.

if(head != null)
{
    Node currentNode = head;
    while(currentNode != null)
    {
        System.out.print(currentNode.getData());
        if(currentNode.getNext()==null)
        {
            currentNode=null;
        }
        else
        {
            currentNode = currentNode.getNext().getNext();
        }
    }
}

The if statement inside the while loop checks that that the next node is not null, meaning there will not be a NullPointerException.

cornstap
  • 21
  • 2
1

First of all, you don't need your initial if/else statement. If you look a bit closer at your while loop, you'll see that you have the same condition there. The loop will handle the null case on its own. Here's a function that prints every other element:

static void printEveryOther( Node head) {  
    int count = 0; 
    while (head != null) {  

        // when count is even print the nodes  
        if (count % 2 == 0)  
            System.out.print(head.getData() + " ");  

        // count the nodes  
        count++;  

        // move on the next node.  
        head = head.getNext();  
    }  
} 

I adapted the code from here.

Keeping in line with your original idea, you could also do the following:

static void printEveryOther( Node head) {  

    while (head != null) {  

        System.out.print(head.getData() + " ");  

        // move on the next node  
        head = head.getNext(); 
        // and onto the node after that, as long as the current isn't null
        if (head != null) 
            head = head.getNext();

    }  
} 

Hope those help.

NickChris
  • 71
  • 5