Minimize first node of Linked List by deleting first or adding one deleted node at start
Given a singly linked list, and an integer K, the task is to make the first node value as minimum as possible in K operations where in each operation:
- Select the first node of the linked list and remove it.
- Add a previously removed node at the start of the linked list.
Examples:
Input: list: 1->4->2->5->3, K=4
Output:1
Explanation:
1st operation-> Remove 1st Node, After performing operation linked list become 4->2->5->3
2nd operation-> Remove 1st Node, After performing operation linked list become 2->5->3
3rd operation-> Remove 1st Node, After performing operation linked list become 5->3
4th operation-> Add previously removed Node (i.e. 1), After performing operation linked list become 1->5->3
Input: Linked List: 5, K = 1
Output: -1
Explanation: Only possible operation is to delete the first node.
If that operation is performed then the list will be empty.
Approach: The problem can be solved by using below observation:
In first K-1 operations, the K-1 value of the starting can be removed. So currently at Kth node. Now in the last operation there are two possible choice:
- Either remove the current starting node (optimal if the value of the (K+1)th node is smaller than the smallest amongst first K-1 already removed elements)
- Add the smallest from the already removed K-1 elements (optimal when the (K+1)th node has higher value than the smallest one)
Follow the below steps to solve the problem:
- If K = 0, then return First Node Value.
- If K = 1, then return Second Node value(if any) else return -1 (because after K operations the list does not exist).
- If the size of the linked list is one then in every odd operation (i.e. 1, 3, 5, . . . ), return -1, else return the first node value (for the same reason as above).
- If K > 2, then:
- Traverse first K-1 nodes and find out the minimum value.
- Compare that minimum value with the (K+1)th node value.
- If (K+1)th value is less than the previous minimum value, update it with (K+1)th Node value.
- Return the minimum value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node( int x)
{
data = x;
next = NULL;
}
};
void push( struct Node** head_ref,
int new_data)
{
struct Node* new_node = new Node(new_data);
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int FirstNode(Node* head, int K)
{
if (K == 0) {
return head->data;
}
if (K == 1) {
return (head->next == NULL) ? -1 : head->next->data;
}
if (head->next == NULL) {
return (K % 2) ? -1 : head->data;
}
int ans = INT_MAX;
int i = 0;
while (head != NULL && i < K - 1) {
if (head->data < ans)
ans = head->data;
head = head->next;
i++;
}
if (head && head->next != NULL) {
ans = min(ans, head->next->data);
}
return ans;
}
int main()
{
int K = 4;
struct Node* head = NULL;
push(&head, 3);
push(&head, 5);
push(&head, 2);
push(&head, 4);
push(&head, 1);
cout << FirstNode(head, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
class Node {
int data;
Node next;
Node( int data)
{
this .data = data;
this .next = null ;
}
}
Node head;
public void push( int data)
{
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
public int firstNode( int k)
{
int ans = Integer.MAX_VALUE;
int i = 0 ;
if (k == 0 ) {
return head.data;
}
if (k == 1 ) {
return (head.next == null ) ? - 1
: head.next.data;
}
if (head.next == null ) {
if (k % 2 == 1 ) {
return - 1 ;
}
return head.data;
}
while (head != null && (i < k - 1 )) {
if (head.data < ans) {
ans = head.data;
}
head = head.next;
i++;
}
if (head != null && head.next != null ) {
ans = Math.min(ans, head.next.data);
}
return ans;
}
public static void main(String[] args)
{
GFG list = new GFG();
list.push( 3 );
list.push( 5 );
list.push( 2 );
list.push( 4 );
list.push( 1 );
int k = 4 ;
System.out.print(list.firstNode(k));
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def FirstNode( self , K):
if (K = = 0 ):
return self .head.data
elif (K = = 1 ):
if ( self .head. next = = None ):
return - 1
return self .head. next .data
elif ( self .head. next = = None ):
if (K % 2 = = 1 ):
return - 1
return self .head.data
ans = 1000000000000000000
i = 0
while ( self .head ! = None and i < (K - 1 )):
if ( self .head.data < ans):
ans = self .head.data
self .head = self .head. next
i + = 1
if ( self .head ! = None and self .head. next ! = None ):
ans = min (ans, self .head. next .data)
return ans
if __name__ = = '__main__' :
K = 4
llist = LinkedList()
llist.push( 3 )
llist.push( 5 )
llist.push( 2 )
llist.push( 4 )
llist.push( 1 )
print (llist.FirstNode(K))
|
C#
using System;
public class GFG {
class Node {
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
Node head;
public void push( int data)
{
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
public int firstNode( int k)
{
int ans = Int32.MaxValue;
int i = 0;
if (k == 0) {
return head.data;
}
if (k == 1) {
return (head.next == null ) ? -1
: head.next.data;
}
if (head.next == null ) {
if (k % 2 == 1) {
return -1;
}
return head.data;
}
while (head != null && (i < k - 1)) {
if (head.data < ans) {
ans = head.data;
}
head = head.next;
i++;
}
if (head != null && head.next != null ) {
ans = Math.Min(ans, head.next.data);
}
return ans;
}
static public void Main()
{
GFG list = new GFG();
list.push(3);
list.push(5);
list.push(2);
list.push(4);
list.push(1);
int k = 4;
Console.WriteLine(list.firstNode(k));
}
}
|
Javascript
<script>
class Node{
constructor(d){
this .data = d
this .next = null
}
}
class LinkedList{
constructor(){
this .head = null
}
push(new_data){
let new_node = new Node(new_data)
new_node.next = this .head
this .head = new_node
}
FirstNode(K){
if (K == 0)
return this .head.data
else if (K == 1){
if ( this .head.next == null )
return -1
return this .head.next.data
}
else if ( this .head.next == null ){
if (K%2==1)
return -1
return this .head.data
}
let ans = 1000000000000000000
let i = 0
while ( this .head != null && i < (K-1)){
if ( this .head.data < ans)
ans = this .head.data
this .head = this .head.next
i+=1
}
if ( this .head != null && this .head.next != null )
ans = Math.min(ans, this .head.next.data)
return ans
}
}
let K = 4
let llist = new LinkedList()
llist.push(3)
llist.push(5)
llist.push(2)
llist.push(4)
llist.push(1)
document.write(llist.FirstNode(K), "</br>" )
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Last Updated :
18 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...