Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Delete Node Of Linked List

Easy
0/40
Average time to solve is 10m
profile
Contributed by
49 upvotes

Problem statement

You are given the head of a linked list ‘list’ of size ‘N’.


Your task is to delete the linked list's last node and return the linked list's head.


Example:
Input: ‘N’ = 4, ‘list’ = 4 -> 2 -> 5 -> 1

Output: 4 2 5

Explanation: 
After deleting the last node, the Linked List is 4 -> 2 -> 5.
Detailed explanation ( Input/output format, Notes, Images )
Input Format
The first line contains one integer, ‘N’, denoting the size of the linked list ‘list’.

The following line contains ‘N’ integers, denoting the elements of the ‘list’.


Output format:
Return the head of the new linked list.


Note:
You don't need to print anything. Just implement the given function. Contents of your returned linked list will be printed.
Sample Input 1:
4
4 2 5 1
Sample Output 1 :
4 2 5
Explanation Of Sample Input 1:
Input: ‘N' = 4, ‘list’ = 4 -> 2 -> 5 -> 1

Output: 4 2 5

Explanation: 
After deleting the last node, the Linked List is 4 -> 2 -> 5.
Sample Input 2:
5
4 3 2 1 5
Sample Output 2:
4 3 2 1
Explanation Of Sample Input 2:
Input: ‘N’ = 5, ‘list’ = 4 -> 3 -> 2 -> 1 -> 5

Output: 4 3 2 1

Explanation: 
After deleting the last node, the Linked List is 4 -> 3 -> 2 -> 1.


Expected Time Complexity:
Try solving this in O(N).


Constraints:
2 <= 'N' <= 10^4
0 <= list elements <= 10^5

Time Limit: 1 sec
Full screen
Console