914. Deletion of the tail of Linked List

Given the head of a singly linked list, delete the tail of the linked list and return the head of the modified list.

The tail is the last node of the linked list.

Example 1:

Input: linkedList = [1, 2, 3]

Output: [1, 2]

Explanation:

The last node was removed.

Example 2:

Input: linkedList = [1]

Output: []

Explanation:

Note that the value of head is null here.

Now Your Turn!

Pick the correct output for the given input

Input: linkedList = [7, 8]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= number of nodes in the Linked List <= 1000
  • 0 <= ListNode.val <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/*
Definition of singly linked list:
class ListNode{
public:
int data;
ListNode *next;
ListNode() : data(0), next(nullptr) {}
ListNode(int x) : data(x), next(nullptr) {}
ListNode(int x, ListNode *next) : data(x), next(next) {}
};
*/
 
class Solution {
public:
ListNode* deleteTail(ListNode* &head) {
//your code goes here
}
};
Test Case

Input:

Linked List