### How to insert Node at beginning of a Linked List

• Two Senerios
1. List is Empty
2. List isnâ€™t Empty

#include<iostream>
using std::cout; using std::endl;
using std::cin;

struct Node{
int data;
Node* next;
};

Node* head = NULL; // Global Variable, can be accessed anywhere

void Insert(int x)
{
Node* temp = new Node();
temp->data = x;

}

void print()
{

// Using a temp variable so we don't modify
cout << "List is: ";

while(temp != NULL)
{    // Iterate through
cout<<temp->data<<"->";
temp=temp->next;
}
cout<< endl;
}

int main()
{

cout << "How Many numbers? \n";
int n, i, x;

cin >> n;

for (i = 0; i <n; i++ ){
cout << "Enter the number \n";
cin >> x;
Insert(x);
print(); // Print out Current linked List
}

}

Lets say we want 5 Nodes and we want to insert the numbers 3,4,10,68, and 12

Output

How Many numbers?
5
Enter the number
3
List is: 3->
Enter the number
4
List is: 4->3->
Enter the number
10
List is: 10->4->3->
Enter the number
68
List is: 68->10->4->3->
Enter the number
12
List is: 12->68->10->4->3->

### Linked List in C/C++ - Insert a node at nth position

• Insert Node at any postion in the linked list

#include<iostream>
using std::cout; using std::endl;
using std::cin;

struct Node{
int data;
Node* next;
};

Node* head =  NULL; // Global Variable, can be accessed anywhere (Empty List)

void Insert(int data, int n)
{
Node* temp1 = new Node();
temp1->data = data;
temp1->next = NULL;

if (n == 1){
// Like Regular Insert at Begining  Code
return; // Finish, exit
}
// Else, were inserting at another position other than Begining

for (int i = 0; i < n-2; i++ )
{
// Find the position ONE BEFORE where we want to insert
temp2 = temp2->next;   // Store Address
}
temp1->next = temp2->next;
temp2->next = temp1;
}

void print()
{

cout << "List is: ";

while(temp!=NULL){

cout<<temp->data<<"->";
temp=temp->next;
}
cout<< endl;
}

int main()
{
Insert(2,1); // List: 2
Insert(3,2); // List: 2, 3
print();
Insert(4,1); // List; 4,2,3
print();
Insert(5,2); // List: 4,5,2,3
print();
Insert(16,4); // List; 4,5,2,3,69
Insert(69,5); // List; 4,5,2,3,6
Insert(420,1);

print();
}

Output:

List is: 2->3->
List is: 4->2->3->
List is: 4->5->2->3->
List is: 420->4->5->2->16->69->3->

### Linked List in C/C++ - Delete a node at nth position

• Insert Notes
#include<iostream>
using std::cout; using std::endl;
using std::cin;

struct Node{
int data;
Node* next;
};

Node* head =  NULL; // Global Variable, can be accessed anywhere (Empty List)

void Insert(int data, int n)
{
Node* temp1 = new Node();
temp1->data = data;
temp1->next = NULL;

if (n == 1){
// Like Regular Insert at Begining  Code
return; // Finish, exit
}
// Else, were inserting at another position other than Begining

for (int i = 0; i < n-2; i++ )
{
// Find the position ONE BEFORE where we want to insert
temp2 = temp2->next;   // Store Address
}
temp1->next = temp2->next;
temp2->next = temp1;
}

void Delete(int n)
{

if (n == 1)
{
delete(temp1); // Remove memory From HEAP
return; // after we execute
}

for (int i = 0; i < n-2; i++ )
{
temp1 = temp1->next;
}

Node* temp2 = temp1->next; // nth Node
temp1->next = temp2->next; // (n+1)th Node
delete(temp2); // Delete temp2

}

void print()
{

cout << "List is: ";

while(temp!=NULL){

cout<<temp->data<<"->";
temp=temp->next;
}
cout<< endl;
}

int main()
{
Insert(2,1); // List: 2
Insert(3,2); // List: 2, 3
print();
Insert(4,1); // List; 4,2,3
print();
Insert(5,2); // List: 4,5,2,3
print();
Insert(16,4); // List; 4,5,2,3,69
Insert(69,5); // List; 4,5,2,3,6
Insert(420,1);

print();

// Lets try to delete

int position;
cout << "Enter a Position to Delete: ";

cin >> position;

Delete(position);

print();
}

Output

List is: 2->3->
List is: 4->2->3->
List is: 4->5->2->3->
List is: 420->4->5->2->16->69->3->
Enter a Position to Delete: 3
List is: 420->4->2->16->69->3->
List is: 2->3->
List is: 4->2->3->
List is: 4->5->2->3->
List is: 420->4->5->2->16->69->3->
Enter a Position to Delete: 1
List is: 4->5->2->16->69->3->

#include<iostream>
using std::cout;
using std::endl;
using std::cin;

/*
Auther: Devin Powers
Project: Reverse a Linked List -iterative
Data: August 8th 2021

*/

struct Node{
int data;
Node* next;
};

void Insert(int x)
{
Node* temp = new Node(); // Create New Node to Insert
temp->data = x;
}

void print()
{

cout << "List is: ";

while(temp!=NULL){

cout<<temp->data<<"->";
temp=temp->next;
}
cout<< endl;
}

void Reverse()
{
Node *current, *next, *previous;
previous = NULL;

// Run loop
while (current != NULL)
{
next = current->next;
current->next = previous; // *(current).next
previous = current; // resetting previous pointers
current = next;

}
// When Current is = NULL
}

int main()
{
Insert(5);
Insert(6);
Insert(4);
Insert(2);
cout << "Before Reversing Linked List: " << endl;
print();
cout << "After Reversing Linked List: " << endl;
Reverse();
print();
}

Output:

List is: 2->4->6->5->
List is: 5->6->4->2->
#include<iostream>
using std::cout;
using std::endl;

struct Node{
int data;
Node* next;
};

void print_forward(Node* p)
{
// Using Recurison
if (p == NULL)
{
return; // Exit Condtion
}
cout << p->data << " -> "; // prints value in the node
print_forward(p->next); // Recusive Call
cout << endl;

}
void print_reverse(Node* p)
{
// Using Recursion
if (p == NULL)
{
return;
}
print_reverse(p->next);  // Recursive Call
cout << p->data << " -> ";
}

void Insert(int x)
{
Node* temp = new Node(); // Create New Node to Insert
temp->data = x;
}

int main(){

Insert(5);
Insert(6);
Insert(4);
Insert(2);

cout << "Print Linked List Forward (Recusion) " << endl;

cout << "Print Linked List Reverse (Recusion) " << endl;
cout << endl;
}

Output:

2 -> 4 -> 6 -> 5 ->

5 -> 6 -> 4 -> 2 ->

### Reverse a linked list using Recursion

#include<iostream>
using std::cout;
using std::endl;

struct Node{
int data;
Node* next;
};

void Insert(int x)
{
Node* temp = new Node(); // Create New Node to Insert
temp->data = x;
}

void Reverse(Node* p)
{
if (p->next == NULL)
{
//Exit condition
return;
}
Reverse(p->next);
Node* q = p->next;
q->next = p;
p->next = NULL;
}

while(temp!=NULL){
cout<<temp->data<<"->";
temp=temp->next;
}
cout<< endl;
}
int main()
{

Insert(5);
Insert(6);
Insert(4);
Insert(2);
cout << "Print Before Reversed " << endl;

cout << "Print Linked List" << endl;