Remove last node of the linked list
Given a linked list, the task is to remove the last node of the linked list and update the head pointer of the linked list. Show Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL Output: 1 -> 2 -> 3 -> 4 -> NULL Explanation: The last node of the linked list is 5, so 5 is deleted. Input: 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULL Output: 2 -> 4 -> 6 -> 8 -> 33 -> NULL Explanation: The last node of the linked list is 67, so 67 is deleted.Recommended: Please try your approach on {IDE} first, before moving on to the solution. Approach: To delete the last node of a linked list, find the second last node and make the next pointer of that node null. Algorithm: 1. If the first node is null or there is only one node, then they return null. if headNode == null then return null if headNode.nextNode == null then free head and return null2. Create an extra space secondLast, and traverse the linked list till the second last node. while secondLast.nextNode.nextNode != null secondLast = secondLast.nextNode3. delete the last node, i.e. the next node of the second last node delete(secondLast.nextNode), and set the value of the next second-last node to null. Implementation: C++
Java
Python3
C#
Javascript
Output: 8 23 11 29 Complexity Analysis:
Article Tags : Data Structures Linked List Technical Scripter Technical Scripter 2018 Practice Tags : Data Structures Linked List LinkedList removeLast() Method in Java
The Java.util.LinkedList.removeLast() method is used to remove the last element from the LinkedList. This method also returns the element after removing it. Parameters: This function does not take any parameters. Java
Output: LinkedList:[Geeks, for, Geeks, 10, 20] The last element is removed: 20 Final LinkedList:[Geeks, for, Geeks, 10] The last element is removed: 10 Final LinkedList:[Geeks, for, Geeks] Time complexity of removeLast() : O(1) Article Tags : Java Java - util package Java-Collections Java-Functions java-LinkedList Practice Tags : Java Java-Collections Remove Last In Linked Listeasy Prev Next Try First, Check Solution later1. You should first read the question and watch the question video.2. Think of a solution approach, then try and submit the question on editor tab. 3. We strongly advise you to watch the solution video for prescribed approach. 1. You are given a partially written LinkedList class. 2. Here is a list of existing functions: 2.1 addLast - adds a new element with given value to the end of Linked List 2.2. display - Prints the elements of linked list from front to end in a single line. All elements are separated by space 2.3. size - Returns the number of elements in the linked list. 2.4. removeFirst - Removes the first element from Linked List. 2.5. getFirst - Returns the data of first element. 2.6. getLast - Returns the data of last element. 2.7. getAt - Returns the data of element available at the index passed. 2.8. addFirst - adds a new element with given value in front of linked list. 2.9. addAt - adds a new element at a given index. 3. You are required to complete the body of removeLast function. This function should remove the last element and update appropriate data members. If the size is 0, should print "List is empty". If the size is 1, should set both head and tail to null. 4. Input and Output is managed for you.Input FormatInput is managed for youOutput FormatOutput is managed for youQuestion Video Comment |