Move last node to front in linked list

Move last node to front in linked list

Here given code implementation process.

//C Program
//Move last node to front in linked list
#include <stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node
{
  int data;
  struct Node*next;
};

//insert Node element
void insert(struct Node**head,int value)
{
  //Create dynamic node
  struct Node*node=(struct Node*)malloc(sizeof(struct Node));
  if(node==NULL)
  {
    printf("Memory overflow\n");
  }
  else
  {
    node->data=value;
    node->next=NULL;
    if(*head==NULL)
    {
      *head=node;
    }
    else
    {
      struct Node*temp=*head;
      //find last node
      while(temp->next!=NULL)
      {
        temp=temp->next;
      }
      //add node at last possition
      temp->next=node;
    }
  }
}
//Display element of Node
void display(struct Node*temp)
{
  
  if(temp==NULL)
  {
    printf("Empty linked list");
  }
  while(temp!=NULL)
  {
    printf("%d  ",temp->data);
    temp=temp->next;
  }
}

struct Node* last_head(struct Node*head)
{
  if(head==NULL) return NULL;

  struct Node*temp=head;
  //find second last node
  while(temp!=NULL && temp->next!=NULL && temp->next->next != NULL)
  {
   temp=temp->next;
  }
  if(head==temp && head->next!=NULL)
  {
    //When only two nodes in linked list
    temp=head->next;
    head->next=NULL;
    temp->next=head;
    head=temp;
  }
  else if(head->next!=NULL)
  {
    //more then two nodes
    temp->next->next=head;
    //make new head
    head=temp->next;
    temp->next=NULL;
  }
  return head;

}

int main()
{
  //Create node pointer
  struct Node*head=NULL;

  //insert element of linked list
  insert(&head,1);
  insert(&head,2);
  insert(&head,3);
  insert(&head,4);
  insert(&head,5);
  insert(&head,6);
  insert(&head,7);

  //display all node
  display(head);

  //make last node as head in linked list
  head=last_head(head);

  printf("\n");
  display(head);
  return 0;
}

Output

1  2  3  4  5  6  7  
7  1  2  3  4  5  6
//C++ Program 
//Move last node to front in linked list
#include <iostream>
using namespace std;

//Create structure
struct Node
{
  int data;
  struct Node*next;
};
class LinkedList
{

public:
  Node*head;//head node
  LinkedList();
  void insert(int);
  void display();
  void last_head();
};
LinkedList::LinkedList()
{
  head=NULL;
}
//insert node at end of linked list
void LinkedList::insert(int value)
{
  //Create dynamic node
  Node*node=new Node;
  if(node==NULL)
  {
    cout<<"Memory overflow\n";
  }else
  {
    node->data=value;
    node->next=NULL;
    if(head==NULL)
    {
      //base condition
      head=node;
    }else
    {
      Node*temp=head;
      while(temp->next!=NULL)
      {
        temp=temp->next;
      }
      //add newly node at last
      temp->next=node;
    }
  }
}
//display all node value in linked list
void LinkedList:: display()
{
  if(head==NULL)
  {
    cout<<"Empty linked list";
  }
  else
  {
    Node*temp=head;
    cout<<"Linked List : ";
    while(temp!=NULL)
    {
      //print node value
      cout<<temp->data<<" ";
      temp=temp->next;
    }
  }
}
void LinkedList :: last_head()
{
  
  if(head==NULL)
  {
    return ;
  }
  
  Node*temp=head;
  //find second last node
  while(temp!=NULL && temp->next!=NULL && temp->next->next != NULL)
  {
   temp=temp->next;
  }
  if(head==temp && head->next!=NULL)
  {
    //When only two nodes in linked list
    temp=head->next;
    head->next=NULL;
    temp->next=head;
    head=temp;
  }
  else if(head->next!=NULL)
  {
    //more then two nodes
    temp->next->next=head;
    //make new head
    head=temp->next;
    temp->next=NULL;
  }
  cout<<endl;
}

int main()
{
  //create object
  LinkedList obj;

  //insert element of linked list
  obj.insert(1);
  obj.insert(2);
  obj.insert(3);
  obj.insert(4);
  obj.insert(5);
  obj.insert(6);
  obj.insert(7);

  //display all node
  obj.display();

  //make last node as head in linked list
  obj.last_head();

  obj.display();
  return 0;
}

Output

Linked List : 1 2 3 4 5 6 7 
Linked List : 7 1 2 3 4 5 6
//Java program
//Move last node to front in linked list
public class LinkedList
{

  static class Node
  {
    int data;
    Node next;
  }
  public Node head;
  //Class constructors
  LinkedList()
  {
    head=null;
  } 
  //insert  element
  public void insert(int value)
  {
    //Create  node
    Node node=new Node();
    node.data=value;
    node.next=null;
    if(head==null) head=node;
    else
    {
      Node temp=head;
      //find last node
      while(temp.next!=null)
      {
        temp=temp.next;
      }
      temp.next=node;
    }
    
  }
  //Display all Linked List elements
  public void display()
  {
    if(head!=null)
    {
      System.out.print("Linked List Element :");
      Node temp=head;
      while(temp!=null)
      {
        System.out.print("  "+temp.data);
        temp=temp.next;
      }
      System.out.println();
    }
    else
    {
      System.out.println("Empty Linked list"); 
    }
  }
  public void last_head()
  {

    if(head==null)
    {
      return ;
    }

    Node temp=head;
    //find second last node
    while(temp!=null && temp.next!=null && temp.next.next != null)
    {
      temp=temp.next;
    }
    if(head==temp && head.next!=null)
    {
      //When only two nodes in linked list
      temp=head.next;
      head.next=null;
      temp.next=head;
      head=temp;
    }
    else if(head.next!=null)
    {
      //more then two nodes
      temp.next.next=head;
      //make new head
      head=temp.next;
      temp.next=null;
    }
    System.out.println();
  }


  public static void main(String[] args) 
  {

    LinkedList obj = new LinkedList();
    //insert element of linked list
    obj.insert(1);
    obj.insert(2);
    obj.insert(3);
    obj.insert(4);
    obj.insert(5);
    obj.insert(6);
    obj.insert(7);

    //display all node
    obj.display();

    //make last node as head in linked list
    obj.last_head();

    obj.display();
  }
}

Output

Linked List Element :  1  2  3  4  5  6  7

Linked List Element :  7  1  2  3  4  5  6
Make last node as head in linked list
#Python Program 
#Move last node to front in linked list
class Node:
  def __init__(self,data):
    self.data=data
    self.next=None

#create class Linked    
class LinkedList:

  def __init__(self):

    #Assign default value
    self.head=None

  #insert new node to Linked  
  def insert(self,data):

    node=Node(data)

    if(self.head==None):
      #first element of linked list
      self.head=node
    else:
    
      temp=self.head
      #find middle node of linked list
      while(temp!=None and temp.next!=None ):
        temp=temp.next
      
      #add at last of linked list  
      temp.next=node 
   

  #display all linked list node 
  def display(self):
    temp=self.head
    while(temp!=None):
      print(temp.data,end=" ")
      temp=temp.next
    print()

  def last_head(self):

    if(self.head==None):
      return 
    

    temp=self.head
    #find second last node
    while(temp != None and temp.next != None and temp.next.next != None):
      temp=temp.next
    
    if(self.head == temp and self.head.next != None):
      #When only two nodes in linked list
      temp=self.head.next
      self.head.next=None
      temp.next=self.head
      self.head=temp
    
    elif(self.head.next!=None):
      #more then two nodes
      temp.next.next=self.head
      #make new head
      self.head=temp.next
      temp.next=None
    
    print()

def main():
  obj = LinkedList()
  obj.insert(1)
  obj.insert(2)
  obj.insert(3)
  obj.insert(4)
  obj.insert(5)
  obj.insert(6)
  obj.insert(7)

  #display all node
  obj.display()

  #make last node as head in linked list
  obj.last_head()

  obj.display()

if __name__ =="__main__":
  main()

Output

1 2 3 4 5 6 7 

7 1 2 3 4 5 6 
//c# program
//Move last node to front in linked list
using System;
public class Node
{
  public int data;
  public Node next;
}
public class LinkedList
{


  public Node head;
  //Class constructors
  LinkedList()
  {
    head=null;
  } 
  //insert  element
  public void insert(int value)
  {
    //Create  node
    Node node=new Node();
    node.data=value;
    node.next=null;
    if(head==null) head=node;
    else
    {
      Node temp=head;
      //find last node
      while(temp.next!=null)
      {
        temp=temp.next;
      }
      temp.next=node;
    }

  }
  //Display all Linked List elements
  public void display()
  {
    if(head!=null)
    {
      Console.WriteLine("Linked List Element :");
      Node temp=head;
      while(temp!=null)
      {
        Console.Write("  "+temp.data);
        temp=temp.next;
      }
      Console.WriteLine();
    }
    else
    {
      Console.WriteLine("Empty Linked list"); 
    }
  }
  public void last_head()
  {

    if(head==null)
    {
      return ;
    }

    Node temp=head;
    //find second last node
    while(temp!=null && temp.next!=null && temp.next.next != null)
    {
      temp=temp.next;
    }
    if(head==temp && head.next!=null)
    {
      //When only two nodes in linked list
      temp=head.next;
      head.next=null;
      temp.next=head;
      head=temp;
    }
    else if(head.next!=null)
    {
      //more then two nodes
      temp.next.next=head;
      //make new head
      head=temp.next;
      temp.next=null;
    }
    Console.WriteLine();
  }


  public static void Main(String[] args) 
  {

    LinkedList obj = new LinkedList();
    //insert element of linked list
    obj.insert(1);
    obj.insert(2);
    obj.insert(3);
    obj.insert(4);
    obj.insert(5);
    obj.insert(6);
    obj.insert(7);

    //display all node
    obj.display();

    //make last node as head in linked list
    obj.last_head();

    obj.display();
  }
}

Output

Linked List Element :
  1  2  3  4  5  6  7

Linked List Element :
  7  1  2  3  4  5  6
<?php
//Php program 
//Move last node to front in linked list
class Node
{
  public $data;
  public $next;
  function __construct($data)
  {
    $this->data = $data;
    $this->next = NULL;
  }
}
class LinkedList
{

  public $head;
  function __construct()
  {
    $head=NULL;
  }
  /*
  * Append the Given data value at end of linked list
  * Fun : insert
  * Parm: data value
  *@return None
  */
  function insert($data)
  {
    $newNode=new Node($data); 

    if($this->head==NULL)
    {
      $this->head=$newNode;
    }
    else
    {
      $temp=$this->head;

      //find last node of linked list
      while($temp->next!=NULL)
      {
        $temp=$temp->next;
      }
      //add new node to last of linked list
      $temp->next=$newNode;
    }
  }
  //Display all inserted node in linked list
  function display()
  {
    if($this->head==NULL)
    {
      echo "\nEmpty Linked List\n";
    }
    else
    {
      $temp=$this->head;
      echo "\nLinked List :";
      while($temp!=NULL)
      {
        //display node value
        echo "  ".$temp->data;
        $temp=$temp->next; //visit to next node
      }
    }   
  }
  function last_head()
  {

    if($this->head==NULL)
    {
      return ;
    }

    $temp=$this->head;
  //find second last node
    while($temp!=NULL && $temp->next!=NULL && $temp->next->next != NULL)
    {
      $temp=$temp->next;
    }
    if($this->head==$temp && $this->head->next!=NULL)
    {
    //When only two nodes in linked list
      $temp=$this->head->next;
      $this->head->next=NULL;
      $temp->next=$this->head;
      $this->head=$temp;
    }
    else if($this->head->next!=NULL)
    {
      //more then two nodes
      $temp->next->next=$this->head;
      //make new head
      $this->head=$temp->next;
      $temp->next=NULL;
    }
    echo "\n";
  }
}
function main()
{
  //Make a object of LinkedList class
  $obj= new LinkedList();

  //insert element of linked list
  $obj->insert(1);
  $obj->insert(2);
  $obj->insert(3);
  $obj->insert(4);
  $obj->insert(5);
  $obj->insert(6);
  $obj->insert(7);

  //display all node
  $obj->display();

  //make last node as head in linked list
  $obj->last_head();

  $obj->display();
}
main();
?>

Output


Linked List :  1  2  3  4  5  6  7

Linked List :  7  1  2  3  4  5  6


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved