Delete Tail of DLL

Linked-List Fundamentals (Doubly LL) Easy

Given the head of a doubly linked list, remove the node at the tail of the linked list and return the head of the modified list.


The tail is the last node of the linked list.

Examples:

Input: head -> 1 <-> 2 <-> 3

Output: head -> 1 <-> 2

Explanation: The node with value 3 was removed.

Input: head -> 7

Output: head

Explanation: Note that the head has null value after the removal.

Input: head -> 2 <-> 4

Constraints

  • n == Number of nodes in the linked list
  • 1 <= n <= 100
  • 0 <= ListNode.val <= 100

Hints

  • If the list is empty (head is None), return None immediately, as there is nothing to delete. If the list contains only one node (head.next is None), deleting the tail means returning an empty list (None).
  • Use a pointer to traverse the list starting from the head. Stop when the pointer reaches the second-to-last node, i.e., when the next pointer of the current node points to the tail (node where next is None).

Company Tags

OYO Rooms McKinsey & Company Snowflake Ubisoft Morgan Stanley HCL Technologies Freshworks ARM Flipkart Visa Bain & Company Deloitte Siemens Healthineers Rockstar Games Reddit Airbnb Zomato Dropbox KPMG Shopify Seagate Technology Bungie Zoho Splunk Philips Healthcare TCS Cognizant Accenture Infosys Capgemini Wipro