<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://socialledge.com/sjsu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Arvindallawadi</id>
		<title>Embedded Systems Learning Academy - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://socialledge.com/sjsu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Arvindallawadi"/>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php/Special:Contributions/Arvindallawadi"/>
		<updated>2026-04-10T21:25:12Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.1</generator>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=Interview_Preparation_Linked_List&amp;diff=35606</id>
		<title>Interview Preparation Linked List</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=Interview_Preparation_Linked_List&amp;diff=35606"/>
				<updated>2017-04-01T01:52:54Z</updated>
		
		<summary type="html">&lt;p&gt;Arvindallawadi: /* Removing duplicate elements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A linked list is like an array that can grow without restriction of a fixed-size array.  The disadvantage is that unlike an array that we can access by an index, we have to iterate through the elements of the list.  This is because unlike an array which has contiguous memory, linked list memory is usually scattered.&lt;br /&gt;
&lt;br /&gt;
To show a picture, an array looks like this :&lt;br /&gt;
&amp;lt;code&amp;gt;[0] [1] [2] [3] ...&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A linked list looks like this :&lt;br /&gt;
&amp;lt;code&amp;gt;head --&amp;gt; [0] --&amp;gt;  [1] --&amp;gt; [2] --&amp;gt; [3] --&amp;gt; [NULL]&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each element of the linked list can be scattered in memory and a pointer is used to indicate where in memory you can find the next element.  If the next element doesn't exist, a NULL pointer is used to mark the end.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Singly Linked List==&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct linked_list_node {&lt;br /&gt;
    struct linked_list_node *next;&lt;br /&gt;
    int data;&lt;br /&gt;
} linked_list_t;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
Observations :&lt;br /&gt;
*  '''&amp;lt;code&amp;gt;linked_list_t&amp;lt;/code&amp;gt;''' is used such that when we want to declare the linked-list node, we can just use '''&amp;lt;code&amp;gt;linked_list_t node&amp;lt;/code&amp;gt;''' rather than '''&amp;lt;code&amp;gt;struct linked_list_node node;&amp;lt;/code&amp;gt;'''&lt;br /&gt;
*:  For the '''&amp;lt;code&amp;gt;*next;&amp;lt;/code&amp;gt;''' linked-list pointer, we have to explicitly use a '''&amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt;''' keyword because at that point in time, the '''&amp;lt;code&amp;gt;linked_list_t&amp;lt;/code&amp;gt;''' has not been declared.&lt;br /&gt;
*  The '''&amp;lt;code&amp;gt;int data;&amp;lt;/code&amp;gt;''' is the actual data of a node, which could be '''&amp;lt;code&amp;gt;void*&amp;lt;/code&amp;gt;''' too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
=== Allocation and Adding elements ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Assume this is a source file, such as linked_list.c */&lt;br /&gt;
/* g for &amp;quot;global&amp;quot; variable of this file, and static for &amp;quot;private variable&amp;quot; of this file */&lt;br /&gt;
static linked_list_t *g_head = NULL;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void ll_allocate_head(int  n)&lt;br /&gt;
{&lt;br /&gt;
    g_head = malloc(sizeof(*g_head));&lt;br /&gt;
    g_head-&amp;gt;data = n;&lt;br /&gt;
    g_head-&amp;gt;next = NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void ll_add_to_beg(int n)&lt;br /&gt;
{&lt;br /&gt;
    if (NULL == g_head) {&lt;br /&gt;
        ll_allocate_head(n);&lt;br /&gt;
    }&lt;br /&gt;
    else {&lt;br /&gt;
        linked_list_t *new_elm = malloc(sizeof(linked_list_t));&lt;br /&gt;
&lt;br /&gt;
        /* Copy the data, and set this node's next to the older head */&lt;br /&gt;
        new_elm-&amp;gt;next = g_head;&lt;br /&gt;
        new_elm-&amp;gt;data = n;&lt;br /&gt;
 &lt;br /&gt;
        /* Head becomes this element since we wanted to add to beginning */&lt;br /&gt;
        g_head = new_elm;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void ll_add_to_end(int n)&lt;br /&gt;
{&lt;br /&gt;
    if (NULL == g_head) {&lt;br /&gt;
        ll_allocate_head(n);&lt;br /&gt;
    }&lt;br /&gt;
    else {&lt;br /&gt;
        /* Need to get to the tail first */&lt;br /&gt;
        linked_list_t *tail = g_head;&lt;br /&gt;
        while(tail-&amp;gt;next != NULL) {&lt;br /&gt;
            tail = tail-&amp;gt;next;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        linked_list_t *new_elm = malloc(sizeof(linked_list_t));&lt;br /&gt;
        new_elm-&amp;gt;next = NULL;&lt;br /&gt;
        new_elm-&amp;gt;data = n;&lt;br /&gt;
&lt;br /&gt;
        /* Simply add new element to the tail */&lt;br /&gt;
        tail-&amp;gt;next = new_elm;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== List Reversal ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
/*Function to reverse a singly linked list by iterating through the list*/&lt;br /&gt;
void ll_reverse(linked_list_t** data)&lt;br /&gt;
{&lt;br /&gt;
    linked_list_t* prev   = NULL;      /*previous node is made NULL*/&lt;br /&gt;
    linked_list_t* g_head= *data;      /*data is assigned to head node*/&lt;br /&gt;
    linked_list_t* next;&lt;br /&gt;
    while (g_head)&lt;br /&gt;
    {&lt;br /&gt;
        next  = g_head-&amp;gt;next;        &lt;br /&gt;
        g_head-&amp;gt;next = prev;   &lt;br /&gt;
        prev = g_head;&lt;br /&gt;
        g_head= next;&lt;br /&gt;
    }&lt;br /&gt;
    *data= prev;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Loop in the List===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
/*Function to reverse a singly linked list by iterating through the list*/&lt;br /&gt;
bool ll_loop(linked_list_t* head)&lt;br /&gt;
{&lt;br /&gt;
    linked_list_t *slow= head, *fast = head;&lt;br /&gt;
    &lt;br /&gt;
    while((slow!=NULL)&amp;amp;&amp;amp;(fast-&amp;gt;next!=NULL)&amp;amp;&amp;amp;(fast!=NULL))//checking to see if the current position or the&lt;br /&gt;
    {                                                           //successive position is pointing to a NULL.&lt;br /&gt;
        slow = slow-&amp;gt; next;         //slow pointer&lt;br /&gt;
        fast = fast-&amp;gt;next-&amp;gt;next;    //fast pointer&lt;br /&gt;
        if(slow==fast){             //comparing to see if the slow and fast pointers meet at a node.&lt;br /&gt;
            return TRUE;            //return true if the loop exists&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return FALSE;                   //return false if the loop doesnt exists.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Removing duplicate elements ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Assume this is a source file, such as linked_list.c */&lt;br /&gt;
/* g for &amp;quot;global&amp;quot; variable of this file, and static for &amp;quot;private variable&amp;quot; of this file */&lt;br /&gt;
static linked_list_t *g_head = NULL;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void remove_dups(linked_list_t* head){ //we are never deleting the head node&lt;br /&gt;
     &lt;br /&gt;
     linked_list_t* current = head;  //current node&lt;br /&gt;
     linked_list_t *pointer1, *duplicate;&lt;br /&gt;
&lt;br /&gt;
     while(current-&amp;gt;next != NULL){//traversing the list pointed by current &lt;br /&gt;
          pointer1 = current;// the current node is copied into pointer1&lt;br /&gt;
          while(pointer1-&amp;gt;next != NULL){// traversing the list pointed by pointer1&lt;br /&gt;
                if(pointer1-&amp;gt;next-&amp;gt;data == current-&amp;gt;data){ //Checking for duplicates by comparing the current data and pointer1 data of next node&lt;br /&gt;
                   duplicate = pointer1-&amp;gt;next;// copy the address of pointer1 node (which contains duplicate data) into duplicate&lt;br /&gt;
                   pointer1-&amp;gt;next = pointer1-&amp;gt;next-&amp;gt;next;//placing the new address i.e. the next node address into pointer to which it will be pointing next&lt;br /&gt;
                   free(duplicate);//free the duplicate node (which contains duplicate data)&lt;br /&gt;
                   }&lt;br /&gt;
                   pointer1 = pointer1-&amp;gt;next;//move to the next node&lt;br /&gt;
                }&lt;br /&gt;
           current = current-&amp;gt;next;//current pointing to next node&lt;br /&gt;
          &lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Palindrome ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Assume this is a source file, such as linked_list.c */&lt;br /&gt;
/* g for &amp;quot;global&amp;quot; variable of this file, and static for &amp;quot;private variable&amp;quot; of this file */&lt;br /&gt;
static linked_list_t *g_head = NULL;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
bool isPalindrome(linked_list_t *head){&lt;br /&gt;
     linked_list_t *reverse = reverse(head);// calling function to reverse the list&lt;br /&gt;
     return checkPalindrome(head,reverse);// calling function to check if the list is Palindrome&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
linked_list_t* reverse(linked_list_t *head1){&lt;br /&gt;
     linked_list_t *temp = NULL;&lt;br /&gt;
&lt;br /&gt;
     while(head1-&amp;gt;next != NULL){&lt;br /&gt;
     linked_list_t *new_reverse_node = (linked_list_t*)malloc(sizeof(linked_list_t));// new node create to copy the current list in reverse order&lt;br /&gt;
     new_reverse_node-&amp;gt;data = head1-&amp;gt;data;// copy head data into new node&lt;br /&gt;
     new_reverse_node-&amp;gt;next = temp;//reversing the list by making the first node the last node&lt;br /&gt;
     temp = new_reverse_node;&lt;br /&gt;
     head1 = head1-&amp;gt;next;//traverse till the end of the list&lt;br /&gt;
     }&lt;br /&gt;
     return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
bool checkPalindrome(linked_list_t *list1, linked_list_t *list2){&lt;br /&gt;
     while(list1-&amp;gt;next != NULL &amp;amp;&amp;amp; list2-&amp;gt;next != NULL){&lt;br /&gt;
     if(list1-&amp;gt;data != list2-&amp;gt;data){&lt;br /&gt;
     return false;&lt;br /&gt;
     }&lt;br /&gt;
     list1 = list1-&amp;gt;next;&lt;br /&gt;
     list2 = list2-&amp;gt;next;&lt;br /&gt;
     }&lt;br /&gt;
     if(list1-&amp;gt;next == NULL &amp;amp;&amp;amp; list2-&amp;gt;next == NULL){&lt;br /&gt;
     return true;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Doubly Linked List ==&lt;br /&gt;
Let's elaborate on our linked list such that we have the '''next''' and the '''previous''' links.  We will have the added benefit of going backwards from an element at the cost of the memory requirement of an extra pointer along with more code to deal with this extra pointer.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct linked_list_node {&lt;br /&gt;
    struct linked_list_node *next;    /*next node*/&lt;br /&gt;
    struct linked_list_node* prev;    /*previous node*/&lt;br /&gt;
    int data;&lt;br /&gt;
} linked_list_t;&lt;br /&gt;
&lt;br /&gt;
/* Assume this is a source file, such as linked_list.c */&lt;br /&gt;
/* g for &amp;quot;global&amp;quot; variable of this file, and static for &amp;quot;private variable&amp;quot; of this file */&lt;br /&gt;
&lt;br /&gt;
static linked_list_t *g_head = NULL;&lt;br /&gt;
&lt;br /&gt;
/*new node is created and a pointer is returned  */&lt;br /&gt;
&lt;br /&gt;
linked_list_t *create_new_node(int n ) {&lt;br /&gt;
  linked_list_t *new_node&lt;br /&gt;
    = malloc(sizeof(linked_list_t));    /*creating a new node*/&lt;br /&gt;
  new_node-&amp;gt;data = n;                   /*assigning input to data*/&lt;br /&gt;
  new_node-&amp;gt;prev = NULL;               /*previous and next links are initialized to NULL*/&lt;br /&gt;
  new_node-&amp;gt;next = NULL;&lt;br /&gt;
  return new_node;        /*returning a pointer*/&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/*Inserting node at head */&lt;br /&gt;
void insert_at_head(int n) {&lt;br /&gt;
  linked_list_t* new_node = create_new_node(n);        /*creating a new node*/&lt;br /&gt;
&lt;br /&gt;
  if(g_head == NULL) {                                 /*condition to check if the head node is empty*/&lt;br /&gt;
    g_head = new_node;                                  &lt;br /&gt;
    return;&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
  g_head-&amp;gt;prev = new_node;   /*assigning new node to head node */       &lt;br /&gt;
  new_node-&amp;gt;next = g_head; &lt;br /&gt;
  g_head = new_node;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/*to insert a node at tail*/&lt;br /&gt;
void insert_at_tail(int n) {&lt;br /&gt;
  linked_list_t* temp = g_head;&lt;br /&gt;
  linked_list_t* new_node = create_new_node(n);        //creating a new node&lt;br /&gt;
&lt;br /&gt;
  if(g_head == NULL) {&lt;br /&gt;
    g_head = new_node;&lt;br /&gt;
    return;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  while(temp-&amp;gt;next != NULL) temp = temp-&amp;gt;next;  //last node&lt;br /&gt;
  temp-&amp;gt;next = new_node;&lt;br /&gt;
  new_node-&amp;gt;prev = temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Delete a node in a linked list==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct linked_list_node {&lt;br /&gt;
    struct linked_list_node *next;    /*next node*/&lt;br /&gt;
    int data;&lt;br /&gt;
} linked_list_t;&lt;br /&gt;
&lt;br /&gt;
void delete_node(linked_list_t *g_head, linked_list_t *N)&lt;br /&gt;
{&lt;br /&gt;
    /* to delete head node */&lt;br /&gt;
    if(g_head == N)&lt;br /&gt;
    {&lt;br /&gt;
        if(g_head-&amp;gt;next == NULL)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        /* Copy next node data to head node */&lt;br /&gt;
        g_head-&amp;gt;data = g_head-&amp;gt;next-&amp;gt;data;&lt;br /&gt;
 &lt;br /&gt;
        /* copy address of next node to N*/&lt;br /&gt;
        N = g_head-&amp;gt;next;&lt;br /&gt;
 &lt;br /&gt;
        /* delete next node */&lt;br /&gt;
        g_head-&amp;gt;next = g_head-&amp;gt;next-&amp;gt;next;&lt;br /&gt;
 &lt;br /&gt;
        free(N);&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    /* to delete a node other than the first(head) node */&lt;br /&gt;
    /*previous node */&lt;br /&gt;
    linked_list_t *prev = g_head;&lt;br /&gt;
    while(prev-&amp;gt;next != NULL &amp;amp;&amp;amp; prev-&amp;gt;next != N)&lt;br /&gt;
        prev = prev-&amp;gt;next;&lt;br /&gt;
 &lt;br /&gt;
   /*check if prev node exists */&lt;br /&gt;
    if(prev-&amp;gt;next == NULL)&lt;br /&gt;
    {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    /*Delete that node from Linked List&lt;br /&gt;
    prev-&amp;gt;next = prev-&amp;gt;next-&amp;gt;next;&lt;br /&gt;
 &lt;br /&gt;
    free(N);&lt;br /&gt;
 &lt;br /&gt;
    return; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Make List unique using STL and recursion ==&lt;br /&gt;
The task - given a list of '''''n''''' integers in a list, remove all the duplicate values so that what remains is a list of unique values.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This example exclusively makes use of STL Library '''''&amp;lt;list&amp;gt;''''' and '''''recursion'''''.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Approach:''''' First define function '''''member_of''''' that checks if a value '''''x'''''' is present in the list.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
bool member_of(const int value, const list&amp;lt;int&amp;gt;&amp;amp; my_list, list&amp;lt;int&amp;gt;::const_iterator it)&lt;br /&gt;
{&lt;br /&gt;
    if (it == my_list.end()) &lt;br /&gt;
    	return false;&lt;br /&gt;
    return (*it == value) || member_of(value, my_list, ++it);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now, take out the first value. Make the rest of the list unique. Then if the value we took out is not in the rest of the list, put it back. Otherwise, leave it out.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void isListUnique(list&amp;lt;int&amp;gt;&amp;amp; alist)&lt;br /&gt;
{&lt;br /&gt;
    if (alist.size() &amp;lt;= 1) return; // base case&lt;br /&gt;
&lt;br /&gt;
    int first = alist.front();&lt;br /&gt;
    alist.erase(alist.begin());  // remove the first element&lt;br /&gt;
&lt;br /&gt;
    isListUnique(alist);  // make the rest of the list unique&lt;br /&gt;
&lt;br /&gt;
    if (!member_of(first, alist, alist.begin()))&lt;br /&gt;
    {&lt;br /&gt;
        alist.push_front(first);  // put back the first element&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
== Reverse a linked list using STL and recursion ==&lt;br /&gt;
'''''Task -''''' Reverse the values of a list of '''''n''''' integers.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This example exclusively using STL library '''''&amp;lt;list&amp;gt;''''' and '''''recursion'''''.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Approach:''''' &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Step 1:''''' Take out the first value of the list. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Step 2:''''' Reverse the rest of the list recursively. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Step 3:''''' Append the removed value to the end of the reversed rest of the list.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void listReverse(list&amp;lt;int&amp;gt;&amp;amp; my_list)&lt;br /&gt;
{&lt;br /&gt;
    if (my_list.size() &amp;lt;= 1) return; // base case&lt;br /&gt;
&lt;br /&gt;
    int first = my_list.front();&lt;br /&gt;
    my_list.erase(my_list.begin());  // remove the first element&lt;br /&gt;
&lt;br /&gt;
    listReverse(my_list);          // reverse the rest of the list&lt;br /&gt;
    my_list.push_back(first);  // append the first element at the end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Arvindallawadi</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=Interview_Preparation_Linked_List&amp;diff=35605</id>
		<title>Interview Preparation Linked List</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=Interview_Preparation_Linked_List&amp;diff=35605"/>
				<updated>2017-04-01T01:50:09Z</updated>
		
		<summary type="html">&lt;p&gt;Arvindallawadi: /* Removing duplicate elements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A linked list is like an array that can grow without restriction of a fixed-size array.  The disadvantage is that unlike an array that we can access by an index, we have to iterate through the elements of the list.  This is because unlike an array which has contiguous memory, linked list memory is usually scattered.&lt;br /&gt;
&lt;br /&gt;
To show a picture, an array looks like this :&lt;br /&gt;
&amp;lt;code&amp;gt;[0] [1] [2] [3] ...&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A linked list looks like this :&lt;br /&gt;
&amp;lt;code&amp;gt;head --&amp;gt; [0] --&amp;gt;  [1] --&amp;gt; [2] --&amp;gt; [3] --&amp;gt; [NULL]&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each element of the linked list can be scattered in memory and a pointer is used to indicate where in memory you can find the next element.  If the next element doesn't exist, a NULL pointer is used to mark the end.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Singly Linked List==&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct linked_list_node {&lt;br /&gt;
    struct linked_list_node *next;&lt;br /&gt;
    int data;&lt;br /&gt;
} linked_list_t;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
Observations :&lt;br /&gt;
*  '''&amp;lt;code&amp;gt;linked_list_t&amp;lt;/code&amp;gt;''' is used such that when we want to declare the linked-list node, we can just use '''&amp;lt;code&amp;gt;linked_list_t node&amp;lt;/code&amp;gt;''' rather than '''&amp;lt;code&amp;gt;struct linked_list_node node;&amp;lt;/code&amp;gt;'''&lt;br /&gt;
*:  For the '''&amp;lt;code&amp;gt;*next;&amp;lt;/code&amp;gt;''' linked-list pointer, we have to explicitly use a '''&amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt;''' keyword because at that point in time, the '''&amp;lt;code&amp;gt;linked_list_t&amp;lt;/code&amp;gt;''' has not been declared.&lt;br /&gt;
*  The '''&amp;lt;code&amp;gt;int data;&amp;lt;/code&amp;gt;''' is the actual data of a node, which could be '''&amp;lt;code&amp;gt;void*&amp;lt;/code&amp;gt;''' too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
=== Allocation and Adding elements ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Assume this is a source file, such as linked_list.c */&lt;br /&gt;
/* g for &amp;quot;global&amp;quot; variable of this file, and static for &amp;quot;private variable&amp;quot; of this file */&lt;br /&gt;
static linked_list_t *g_head = NULL;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void ll_allocate_head(int  n)&lt;br /&gt;
{&lt;br /&gt;
    g_head = malloc(sizeof(*g_head));&lt;br /&gt;
    g_head-&amp;gt;data = n;&lt;br /&gt;
    g_head-&amp;gt;next = NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void ll_add_to_beg(int n)&lt;br /&gt;
{&lt;br /&gt;
    if (NULL == g_head) {&lt;br /&gt;
        ll_allocate_head(n);&lt;br /&gt;
    }&lt;br /&gt;
    else {&lt;br /&gt;
        linked_list_t *new_elm = malloc(sizeof(linked_list_t));&lt;br /&gt;
&lt;br /&gt;
        /* Copy the data, and set this node's next to the older head */&lt;br /&gt;
        new_elm-&amp;gt;next = g_head;&lt;br /&gt;
        new_elm-&amp;gt;data = n;&lt;br /&gt;
 &lt;br /&gt;
        /* Head becomes this element since we wanted to add to beginning */&lt;br /&gt;
        g_head = new_elm;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void ll_add_to_end(int n)&lt;br /&gt;
{&lt;br /&gt;
    if (NULL == g_head) {&lt;br /&gt;
        ll_allocate_head(n);&lt;br /&gt;
    }&lt;br /&gt;
    else {&lt;br /&gt;
        /* Need to get to the tail first */&lt;br /&gt;
        linked_list_t *tail = g_head;&lt;br /&gt;
        while(tail-&amp;gt;next != NULL) {&lt;br /&gt;
            tail = tail-&amp;gt;next;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        linked_list_t *new_elm = malloc(sizeof(linked_list_t));&lt;br /&gt;
        new_elm-&amp;gt;next = NULL;&lt;br /&gt;
        new_elm-&amp;gt;data = n;&lt;br /&gt;
&lt;br /&gt;
        /* Simply add new element to the tail */&lt;br /&gt;
        tail-&amp;gt;next = new_elm;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== List Reversal ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
/*Function to reverse a singly linked list by iterating through the list*/&lt;br /&gt;
void ll_reverse(linked_list_t** data)&lt;br /&gt;
{&lt;br /&gt;
    linked_list_t* prev   = NULL;      /*previous node is made NULL*/&lt;br /&gt;
    linked_list_t* g_head= *data;      /*data is assigned to head node*/&lt;br /&gt;
    linked_list_t* next;&lt;br /&gt;
    while (g_head)&lt;br /&gt;
    {&lt;br /&gt;
        next  = g_head-&amp;gt;next;        &lt;br /&gt;
        g_head-&amp;gt;next = prev;   &lt;br /&gt;
        prev = g_head;&lt;br /&gt;
        g_head= next;&lt;br /&gt;
    }&lt;br /&gt;
    *data= prev;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Loop in the List===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
/*Function to reverse a singly linked list by iterating through the list*/&lt;br /&gt;
bool ll_loop(linked_list_t* head)&lt;br /&gt;
{&lt;br /&gt;
    linked_list_t *slow= head, *fast = head;&lt;br /&gt;
    &lt;br /&gt;
    while((slow!=NULL)&amp;amp;&amp;amp;(fast-&amp;gt;next!=NULL)&amp;amp;&amp;amp;(fast!=NULL))//checking to see if the current position or the&lt;br /&gt;
    {                                                           //successive position is pointing to a NULL.&lt;br /&gt;
        slow = slow-&amp;gt; next;         //slow pointer&lt;br /&gt;
        fast = fast-&amp;gt;next-&amp;gt;next;    //fast pointer&lt;br /&gt;
        if(slow==fast){             //comparing to see if the slow and fast pointers meet at a node.&lt;br /&gt;
            return TRUE;            //return true if the loop exists&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return FALSE;                   //return false if the loop doesnt exists.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Removing duplicate elements ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Assume this is a source file, such as linked_list.c */&lt;br /&gt;
/* g for &amp;quot;global&amp;quot; variable of this file, and static for &amp;quot;private variable&amp;quot; of this file */&lt;br /&gt;
static linked_list_t *g_head = NULL;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void remove_dups(linked_list_t* g_head){ //we are never deleting the head node&lt;br /&gt;
     &lt;br /&gt;
     linked_list_t* current = g_head;  //current node&lt;br /&gt;
     linked_list_t *pointer1, *duplicate;&lt;br /&gt;
&lt;br /&gt;
     while(current-&amp;gt;next != NULL){//traversing the list pointed by current &lt;br /&gt;
          pointer1 = current;// the current node is copied into pointer1&lt;br /&gt;
          while(pointer1-&amp;gt;next != NULL){// traversing the list pointed by pointer1&lt;br /&gt;
                if(pointer1-&amp;gt;next-&amp;gt;data == current-&amp;gt;data){ //Checking for duplicates by comparing the current data and pointer1 data of next node&lt;br /&gt;
                   duplicate = pointer1-&amp;gt;next;// copy the address of pointer1 node (which contains duplicate data) into duplicate&lt;br /&gt;
                   pointer1-&amp;gt;next = pointer1-&amp;gt;next-&amp;gt;next;//placing the new address i.e. the next node address into pointer to which it will be pointing next&lt;br /&gt;
                   free(duplicate);//free the duplicate node (which contains duplicate data)&lt;br /&gt;
                   }&lt;br /&gt;
                   pointer1 = pointer1-&amp;gt;next;//move to the next node&lt;br /&gt;
                }&lt;br /&gt;
           current = current-&amp;gt;next;//current pointing to next node&lt;br /&gt;
          &lt;br /&gt;
          }&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Palindrome ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* malloc() */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Assume this is a source file, such as linked_list.c */&lt;br /&gt;
/* g for &amp;quot;global&amp;quot; variable of this file, and static for &amp;quot;private variable&amp;quot; of this file */&lt;br /&gt;
static linked_list_t *g_head = NULL;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
bool isPalindrome(linked_list_t *head){&lt;br /&gt;
     linked_list_t *reverse = reverse(head);// calling function to reverse the list&lt;br /&gt;
     return checkPalindrome(head,reverse);// calling function to check if the list is Palindrome&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
linked_list_t* reverse(linked_list_t *head1){&lt;br /&gt;
     linked_list_t *temp = NULL;&lt;br /&gt;
&lt;br /&gt;
     while(head1-&amp;gt;next != NULL){&lt;br /&gt;
     linked_list_t *new_reverse_node = (linked_list_t*)malloc(sizeof(linked_list_t));// new node create to copy the current list in reverse order&lt;br /&gt;
     new_reverse_node-&amp;gt;data = head1-&amp;gt;data;// copy head data into new node&lt;br /&gt;
     new_reverse_node-&amp;gt;next = temp;//reversing the list by making the first node the last node&lt;br /&gt;
     temp = new_reverse_node;&lt;br /&gt;
     head1 = head1-&amp;gt;next;//traverse till the end of the list&lt;br /&gt;
     }&lt;br /&gt;
     return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
bool checkPalindrome(linked_list_t *list1, linked_list_t *list2){&lt;br /&gt;
     while(list1-&amp;gt;next != NULL &amp;amp;&amp;amp; list2-&amp;gt;next != NULL){&lt;br /&gt;
     if(list1-&amp;gt;data != list2-&amp;gt;data){&lt;br /&gt;
     return false;&lt;br /&gt;
     }&lt;br /&gt;
     list1 = list1-&amp;gt;next;&lt;br /&gt;
     list2 = list2-&amp;gt;next;&lt;br /&gt;
     }&lt;br /&gt;
     if(list1-&amp;gt;next == NULL &amp;amp;&amp;amp; list2-&amp;gt;next == NULL){&lt;br /&gt;
     return true;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Doubly Linked List ==&lt;br /&gt;
Let's elaborate on our linked list such that we have the '''next''' and the '''previous''' links.  We will have the added benefit of going backwards from an element at the cost of the memory requirement of an extra pointer along with more code to deal with this extra pointer.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct linked_list_node {&lt;br /&gt;
    struct linked_list_node *next;    /*next node*/&lt;br /&gt;
    struct linked_list_node* prev;    /*previous node*/&lt;br /&gt;
    int data;&lt;br /&gt;
} linked_list_t;&lt;br /&gt;
&lt;br /&gt;
/* Assume this is a source file, such as linked_list.c */&lt;br /&gt;
/* g for &amp;quot;global&amp;quot; variable of this file, and static for &amp;quot;private variable&amp;quot; of this file */&lt;br /&gt;
&lt;br /&gt;
static linked_list_t *g_head = NULL;&lt;br /&gt;
&lt;br /&gt;
/*new node is created and a pointer is returned  */&lt;br /&gt;
&lt;br /&gt;
linked_list_t *create_new_node(int n ) {&lt;br /&gt;
  linked_list_t *new_node&lt;br /&gt;
    = malloc(sizeof(linked_list_t));    /*creating a new node*/&lt;br /&gt;
  new_node-&amp;gt;data = n;                   /*assigning input to data*/&lt;br /&gt;
  new_node-&amp;gt;prev = NULL;               /*previous and next links are initialized to NULL*/&lt;br /&gt;
  new_node-&amp;gt;next = NULL;&lt;br /&gt;
  return new_node;        /*returning a pointer*/&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/*Inserting node at head */&lt;br /&gt;
void insert_at_head(int n) {&lt;br /&gt;
  linked_list_t* new_node = create_new_node(n);        /*creating a new node*/&lt;br /&gt;
&lt;br /&gt;
  if(g_head == NULL) {                                 /*condition to check if the head node is empty*/&lt;br /&gt;
    g_head = new_node;                                  &lt;br /&gt;
    return;&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
  g_head-&amp;gt;prev = new_node;   /*assigning new node to head node */       &lt;br /&gt;
  new_node-&amp;gt;next = g_head; &lt;br /&gt;
  g_head = new_node;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/*to insert a node at tail*/&lt;br /&gt;
void insert_at_tail(int n) {&lt;br /&gt;
  linked_list_t* temp = g_head;&lt;br /&gt;
  linked_list_t* new_node = create_new_node(n);        //creating a new node&lt;br /&gt;
&lt;br /&gt;
  if(g_head == NULL) {&lt;br /&gt;
    g_head = new_node;&lt;br /&gt;
    return;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  while(temp-&amp;gt;next != NULL) temp = temp-&amp;gt;next;  //last node&lt;br /&gt;
  temp-&amp;gt;next = new_node;&lt;br /&gt;
  new_node-&amp;gt;prev = temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Delete a node in a linked list==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct linked_list_node {&lt;br /&gt;
    struct linked_list_node *next;    /*next node*/&lt;br /&gt;
    int data;&lt;br /&gt;
} linked_list_t;&lt;br /&gt;
&lt;br /&gt;
void delete_node(linked_list_t *g_head, linked_list_t *N)&lt;br /&gt;
{&lt;br /&gt;
    /* to delete head node */&lt;br /&gt;
    if(g_head == N)&lt;br /&gt;
    {&lt;br /&gt;
        if(g_head-&amp;gt;next == NULL)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        /* Copy next node data to head node */&lt;br /&gt;
        g_head-&amp;gt;data = g_head-&amp;gt;next-&amp;gt;data;&lt;br /&gt;
 &lt;br /&gt;
        /* copy address of next node to N*/&lt;br /&gt;
        N = g_head-&amp;gt;next;&lt;br /&gt;
 &lt;br /&gt;
        /* delete next node */&lt;br /&gt;
        g_head-&amp;gt;next = g_head-&amp;gt;next-&amp;gt;next;&lt;br /&gt;
 &lt;br /&gt;
        free(N);&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    /* to delete a node other than the first(head) node */&lt;br /&gt;
    /*previous node */&lt;br /&gt;
    linked_list_t *prev = g_head;&lt;br /&gt;
    while(prev-&amp;gt;next != NULL &amp;amp;&amp;amp; prev-&amp;gt;next != N)&lt;br /&gt;
        prev = prev-&amp;gt;next;&lt;br /&gt;
 &lt;br /&gt;
   /*check if prev node exists */&lt;br /&gt;
    if(prev-&amp;gt;next == NULL)&lt;br /&gt;
    {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    /*Delete that node from Linked List&lt;br /&gt;
    prev-&amp;gt;next = prev-&amp;gt;next-&amp;gt;next;&lt;br /&gt;
 &lt;br /&gt;
    free(N);&lt;br /&gt;
 &lt;br /&gt;
    return; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Make List unique using STL and recursion ==&lt;br /&gt;
The task - given a list of '''''n''''' integers in a list, remove all the duplicate values so that what remains is a list of unique values.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This example exclusively makes use of STL Library '''''&amp;lt;list&amp;gt;''''' and '''''recursion'''''.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Approach:''''' First define function '''''member_of''''' that checks if a value '''''x'''''' is present in the list.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
bool member_of(const int value, const list&amp;lt;int&amp;gt;&amp;amp; my_list, list&amp;lt;int&amp;gt;::const_iterator it)&lt;br /&gt;
{&lt;br /&gt;
    if (it == my_list.end()) &lt;br /&gt;
    	return false;&lt;br /&gt;
    return (*it == value) || member_of(value, my_list, ++it);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now, take out the first value. Make the rest of the list unique. Then if the value we took out is not in the rest of the list, put it back. Otherwise, leave it out.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void isListUnique(list&amp;lt;int&amp;gt;&amp;amp; alist)&lt;br /&gt;
{&lt;br /&gt;
    if (alist.size() &amp;lt;= 1) return; // base case&lt;br /&gt;
&lt;br /&gt;
    int first = alist.front();&lt;br /&gt;
    alist.erase(alist.begin());  // remove the first element&lt;br /&gt;
&lt;br /&gt;
    isListUnique(alist);  // make the rest of the list unique&lt;br /&gt;
&lt;br /&gt;
    if (!member_of(first, alist, alist.begin()))&lt;br /&gt;
    {&lt;br /&gt;
        alist.push_front(first);  // put back the first element&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
== Reverse a linked list using STL and recursion ==&lt;br /&gt;
'''''Task -''''' Reverse the values of a list of '''''n''''' integers.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This example exclusively using STL library '''''&amp;lt;list&amp;gt;''''' and '''''recursion'''''.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Approach:''''' &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Step 1:''''' Take out the first value of the list. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Step 2:''''' Reverse the rest of the list recursively. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''''Step 3:''''' Append the removed value to the end of the reversed rest of the list.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void listReverse(list&amp;lt;int&amp;gt;&amp;amp; my_list)&lt;br /&gt;
{&lt;br /&gt;
    if (my_list.size() &amp;lt;= 1) return; // base case&lt;br /&gt;
&lt;br /&gt;
    int first = my_list.front();&lt;br /&gt;
    my_list.erase(my_list.begin());  // remove the first element&lt;br /&gt;
&lt;br /&gt;
    listReverse(my_list);          // reverse the rest of the list&lt;br /&gt;
    my_list.push_back(first);  // append the first element at the end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Arvindallawadi</name></author>	</entry>

	</feed>