Ad

How To Print The Last Three Nodes Of A Linked List?

My whole code to try to get the las three nodes of a linked list is: Node class:

package com.company;

public class Node {

    public int data;
    public Node nextNode;

    public Node(int data) {
        this.data = data;
        this.nextNode = null;
    }

    public int getData() {
        return data;
    }
}

Linked list class;

package com.company;
public class LinkedList {
    public Node head;
    public int size;

    public LinkedList() {
        this.head = null;
        this.size = 0;
    }

    public void add(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
        } else {
            Node currentNode = head;
            while(currentNode.nextNode != null) {
                currentNode = currentNode.nextNode;
            }
            currentNode.nextNode = node;
        }
        size++;
    }
    
    public void printData() {
        Node currentNode = head;

        while(currentNode != null) {
            int data = currentNode.getData();
            System.out.println(data);
            currentNode = currentNode.nextNode;
        }
    }

    public void printLastThree(){
        Node currentNode = head;
        int i = this.size - 3;
        while(i <= this.size) {
            int data = currentNode.getData();
            System.out.println(data);
            currentNode = currentNode.nextNode;
            i++;
        }
    }
}

Main class:

package com.company;
public class Main {
    public static void main(String[] args) {
        LinkedList ll = new LinkedList();

        ll.add(1);
        ll.add(2);
        ll.add(3);
        ll.add(4);
        ll.add(5);
        ll.add(6);
        ll.add(7);
        ll.add(8);
        ll.add(9);
        ll.add(10);
        ll.add(11);
        ll.add(12);
        
        ll.printData();
        System.out.println();
        ll.printLastThree();
    }
}

As you can see, in the linked list class I try to print the last three nodes of the linked list with the printLastThree() method, but in console I just get:

1
2
3
4

And I would like to get:

10
11
12

Can you say me what I am doing wrong? I try in printLastThree() method to get the total size of the linked list and substract 3 positions, and then get to the total size of the linked list, but that doesn't work. Thanks.

Ad

Answer

Once can see from your code that currentNode starts as the head node, and its value gets printed in the first iteration of the loop. This is not what you want.

You'll first have to skip nodes, which do not get printed. You already calculated how many such nodes need to be skipped (this.size - 3), so you only need to add the loop to actually skip that many nodes:

    public void printLastThree(){
        Node currentNode = head;
        // First SKIP nodes (not to be printed)
        for (int i = size - 3; i > 0; i--) {
            currentNode = currentNode.nextNode;
        }
        // ...and only then start printing
        while (currentNode != null) {
            int data = currentNode.getData();
            System.out.println(data);
            currentNode = currentNode.nextNode;
        }
    }

You can also do it with one loop, and make the printing conditional on the current index:

    public void printLastThree(){
        Node currentNode = head;
        for (int i = 0; i < size; i++) {
            if (i >= size - 3) { // Are we at the last three nodes?
                int data = currentNode.getData();
                System.out.println(data);
            }
            currentNode = currentNode.nextNode;
        }
    }
Ad
source: stackoverflow.com
Ad