Showing posts with label Linked List. Show all posts
Showing posts with label Linked List. Show all posts

Thursday, 21 August 2014

Reverse a Linked List

Reverse a given Linked List.




Implementation
 #include <iostream>
using namespace std;
struct Node
{
    int data;
    Node *next;
};
void Insert ( Node **head,int d )
{
    if ( head==NULL )
    {
        Node *newNode=new Node;
        newNode->data=d;
        newNode->next=NULL;
        *head=newNode;
    }
    else
    {
        Node *newNode=new Node;
        newNode->data=d;
        newNode->next=*head;
        *head = newNode;

    }
}
void Display ( Node *head )
{
    while ( head!=NULL )
    {
        if ( head->next!=NULL )
        {
            cout<<head->data<<"->";
        }
        else {
            cout<<head->data;
        }
        head=head->next;

    }
}

Node * ReverseLinkedList ( Node **head )
{
    Node *p,*q,*r;
    p=*head;
    q= ( *head )->next;
    p->next=NULL;
    while ( q!=NULL )
    {
        r=q->next;
        q->next=p;
        p=q;
        q=r;
    }
    *head=p;
    return *head;

}

int main()
{
    Node *head=NULL;
    Insert ( &head,5 );
    Insert ( &head,25 );
    Insert ( &head,35 );
    Insert ( &head,15 );
    Insert ( &head,20 );
    Insert ( &head,2 );
    Display ( head );
    ReverseLinkedList ( &head );
    cout<<"\n";
    Display ( head );
    getchar();

}

Please comment if you find anything incorrect.

Saturday, 21 June 2014

Find nth Element from last in a Single Linked List

We have to find nth element from last in a given linked list. Below is the algorithm used for the same
  • Take 2 pointers both pointing to head of linked list.
  • Move the first pointer to next element n number of times.
  • Now Start moving the second pointer till first pointer reaches end of linked list.
  • The data in the node of second pointer is your nth element from last.
See  FindFromLast function for implementation of algorithm.


Input:     number of elements in Linked List
              values in number of elements
              n
Output : nth element from last

For Example:

Input:    7
             3 9 4 13 11 7 2
             4

Output  13



#include <iostream>
using namespace std;
struct Node
{
    int data;
    Node *next;
};
Node * Insert ( Node *head ,int data )
{
    if ( head==NULL )
    {
        head=new Node;
        head->data=data;
        head->next=NULL;
        return head;


    }

    else
    {
        Node *q=NULL;
        q=new Node;
        q->data=data;
        q->next=head;
        return q;
    }
}
int FindFromLast ( Node *head,int n )
{
    int i=n,j=1;
    Node *p=head,*q=head;
    while ( j<=n )
    {

        p=p->next;

        j++;
    }
    while ( p!=NULL )
    {
        q=q->next;
        p=p->next;
    }
    return q->data;
}
int main()
{
    Node *start=NULL;
    int n,input,data;
    cin>>input;
    for ( int i=1; i<=input; i++ )
    {
        cin>> data;
        start=Insert ( start,data );
    }
    cin>>n;
    if ( n>input ||n<=0 )
    {
        cout<<"Enter a number less than total elements and greater than zero";
    }
    else
    {
        cout<<FindFromLast ( start,n );
    }

}

Please comment if you find anything incorrect or have any other inputs.