📌  相关文章
📜  Java程序获取链表的第一个和最后一个元素

📅  最后修改于: 2022-05-13 01:54:18.707000             🧑  作者: Mango

Java程序获取链表的第一个和最后一个元素

链表一种线性数据结构,其中元素不存储在连续的内存位置。给定的任务是检索给定链表的第一个和最后一个元素。

链表的属性:

  • 元素以非连续方式存储。
  • 每个元素都是一个对象,其中包含指向下一个元素的指针。
Input: [2, 5, 5, 7, 10, 6]

Output: First element is : 2
        Last element is : 6

Input: [1]

Output: First element is : 1
        Last element is : 1

方法 1:使用内置库

使用Java.util 包中预先构建的LinkedList 类来构建链表,并使用预先定义的方法来获取相应的值。

示例:下面是上述方法的实现:

Java
// Java Program to get the first and the last element of a
// Linked List
 
// Importing the Linked List class from the util package
import java.util.LinkedList;
 
class AccessFirstAndLastElements {
    public static void main(String[] args)
    {
        // Initializing the Linked List
        LinkedList ll = new LinkedList<>();
 
        // Adding elements to the Linked List
        ll.add(2);
        ll.add(5);
        ll.add(5);
        ll.add(7);
        ll.add(10);
        ll.add(6);
 
        // Getting the first element
        System.out.println("First Element is : "
                           + ll.getFirst());
 
        // Getting the last element
        System.out.println("Last Element is : "
                           + ll.getLast());
    }
}


Java
// Java program to get the first and last element from a
// Linked List
 
// A Generic Node class is used to create a Linked List
class Node {
 
    // Data Stored in each Node of the Linked List
    E data;
 
    // Pointer to the next node in the Linked List
    Node next;
 
    // Node class constructor used to initializes the data
    // in each Node
    Node(E data) { this.data = data; }
}
 
class LinkedList {
 
    // Points to the head of the Linked List
    // i.e the first element
    Node head;
    int size = 0;
 
    // Addition of elements to the tail of the Linked List
    public void add(E element)
    {
 
        // Checks whether the head is created else creates a
        // new one
        if (head == null) {
            head = new Node<>(element);
            size++;
            return;
        }
 
        // The Node which needs to be added at
        // the tail of the Linked List
        Node add = new Node<>(element);
 
        // Storing the instance of the
        // head pointer
        Node temp = head;
 
        // The while loop takes us to the tail of the Linked
        // List
        while (temp.next != null) {
 
            temp = temp.next;
        }
 
        // New Node is added at the tail of
        // the Linked List
        temp.next = add;
 
        // Size of the Linked List is incremented as
        // the elements are added
        size++;
    }
 
    // Retrieves the first element of the Linked List
    public E getFirst() throws Exception
    {
 
        // Throws an Exception if the List is empty
        if (head == null) {
            throw new Exception(
                "No elements found in Linked List");
        }
 
        // Returns the first element
        return head.data;
    }
 
    // Retrieves the last element of the Linked List
    public E getLast() throws Exception
    {
        // Throws an Exception if the List is empty
        if (head == null) {
            throw new Exception(
                "No elements found in Linked List");
        }
 
        Node temp = head;
 
        // The while loop takes us to the tail of the Linked
        // List
        while (temp.next != null) {
            temp = temp.next;
        }
 
        // Returns the last element
        return temp.data;
    }
}
 
class AccessFirstAndLastElements {
    public static void main(String[] args) throws Exception
    {
        // Initializing the Linked List
        LinkedList ll = new LinkedList<>();
 
        // Adding elements to the Linked List
        ll.add(2);
        ll.add(5);
        ll.add(5);
        ll.add(7);
        ll.add(10);
        ll.add(6);
 
        // Getting the first element
        System.out.println("First Element is : "
                           + ll.getFirst());
 
        // Getting the last element
        System.out.println("Last Element is : "
                           + ll.getLast());
    }
}


输出
First Element is : 2
Last Element is : 6

时间复杂度: getFirst() 花费 O(1) 时间,getLast() 花费 O(n) 时间,其中n是链表中的元素数。

方法 2:不使用内置方法

  • 创建一个通用的 Node 类。
  • 使用 Node 类创建我们自己的链表类。
  • 创建所需的 add()、getFirst() 和 getLast() 方法。
  • 初始化链表。
  • 使用相应的 get 方法来获取值。

示例:下面是上述方法的实现:

Java

// Java program to get the first and last element from a
// Linked List
 
// A Generic Node class is used to create a Linked List
class Node {
 
    // Data Stored in each Node of the Linked List
    E data;
 
    // Pointer to the next node in the Linked List
    Node next;
 
    // Node class constructor used to initializes the data
    // in each Node
    Node(E data) { this.data = data; }
}
 
class LinkedList {
 
    // Points to the head of the Linked List
    // i.e the first element
    Node head;
    int size = 0;
 
    // Addition of elements to the tail of the Linked List
    public void add(E element)
    {
 
        // Checks whether the head is created else creates a
        // new one
        if (head == null) {
            head = new Node<>(element);
            size++;
            return;
        }
 
        // The Node which needs to be added at
        // the tail of the Linked List
        Node add = new Node<>(element);
 
        // Storing the instance of the
        // head pointer
        Node temp = head;
 
        // The while loop takes us to the tail of the Linked
        // List
        while (temp.next != null) {
 
            temp = temp.next;
        }
 
        // New Node is added at the tail of
        // the Linked List
        temp.next = add;
 
        // Size of the Linked List is incremented as
        // the elements are added
        size++;
    }
 
    // Retrieves the first element of the Linked List
    public E getFirst() throws Exception
    {
 
        // Throws an Exception if the List is empty
        if (head == null) {
            throw new Exception(
                "No elements found in Linked List");
        }
 
        // Returns the first element
        return head.data;
    }
 
    // Retrieves the last element of the Linked List
    public E getLast() throws Exception
    {
        // Throws an Exception if the List is empty
        if (head == null) {
            throw new Exception(
                "No elements found in Linked List");
        }
 
        Node temp = head;
 
        // The while loop takes us to the tail of the Linked
        // List
        while (temp.next != null) {
            temp = temp.next;
        }
 
        // Returns the last element
        return temp.data;
    }
}
 
class AccessFirstAndLastElements {
    public static void main(String[] args) throws Exception
    {
        // Initializing the Linked List
        LinkedList ll = new LinkedList<>();
 
        // Adding elements to the Linked List
        ll.add(2);
        ll.add(5);
        ll.add(5);
        ll.add(7);
        ll.add(10);
        ll.add(6);
 
        // Getting the first element
        System.out.println("First Element is : "
                           + ll.getFirst());
 
        // Getting the last element
        System.out.println("Last Element is : "
                           + ll.getLast());
    }
}
输出
First Element is : 2
Last Element is : 6

时间复杂度:getFirst()花费 O(1) 时间, getLast()花费 O(n) 时间,其中n是链表中的元素数。