Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #30

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions Data Structures/LinkedList/SinglyLinkedList/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Singly Linked List

# Singly Linked List
## Complexity
#### Time:
- Time:
- Access:
- Worst Case: ![formula](https://render.githubusercontent.com/render/math?math=O(n))
Expand All @@ -15,4 +16,32 @@
- Worst Case: ![formula](https://render.githubusercontent.com/render/math?math=O(1))
- Average Case: ![formula](https://render.githubusercontent.com/render/math?math=O(1))
- Space:
- Worst Case: ![formula](https://render.githubusercontent.com/render/math?math=O(n))
- Worst Case: ![formula](https://render.githubusercontent.com/render/math?math=O(n))


# Reverse a linked list


## Demo
Input: Head of following linked list
1->2->3->4->NULL
Output: Linked list should be changed to,
4->3->2->1->NULL

Input: Head of following linked list
1->2->3->4->5->NULL
Output: Linked list should be changed to,
5->4->3->2->1->NULL

## Algorithms
Initialize three pointers prev as NULL, curr as head and next as NULL.
Iterate through the linked list. In loop, do following.
// Before changing next of current,
// store next node
next = curr->next
// Now change next of current
// This is where actual reversing happens
curr->next = prev
// Move prev and curr one step forward
prev = curr
curr = next
217 changes: 217 additions & 0 deletions Data Structures/LinkedList/SinglyLinkedList/ReverseSinglyLinkList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#include<iostream>

using namespace std;


struct node{
int data;
struct node * link;
};

struct node * start;

void insertBegin(int item)
{
node * t= new node;
t->data= item;

// allocate memory for 1 node variable and t will be pointer to that node
if( start== NULL)
{
start= t; t->link= NULL;
}
else
{
t->link= start;
start= t;
}
cout<<" inserted at begin "<<item<<endl;

}

void insertLast(int item)
{
node * t= new node;
t->data= item; t->link= NULL;
if(start== NULL)
{
start= t ;
}
else{

node * p= start;
while(p->link!= NULL)
{
p= p->link;
}
p->link= t ; //p stores adress of current last node

}
cout<<"inserted at last "<<item<<endl;
}
void traverse()
{
node* p= start;
while(p!= NULL)
{
cout<<p->data<<"-> ";
p= p->link;
}
cout<<"NULL"<<endl;
}

void searchItem(int item)
{
node * p= start;
while(p!= NULL)
{
if(item==p->data)
{
cout<<" found "<<item<<endl; return;
}
else
{
p= p->link;
}
}
cout<<" not found "<<endl;
}

void insertAfter(int val, int item)
{
node * t= new node;
t->data= item;
if( start== NULL)
{
start= t; t->link= NULL;
}
else
{
node * p= start;
while(p!= NULL)
{
if(val==p->data)
{
t->link= p->link;
p->link= t;
return;
}
else
{
p=p->link;
}
}

}

}

void deleteBegin()
{
if(start== NULL)
{
cout<<"List is empty "<<endl; return;
}
else if(start->link==NULL)
{
node *p= start;
start= NULL;
delete p;
}
else
{
node *p= start;
start= start->link;
delete p;
}
}
void deleteLast()
{
if(start==NULL)
{
cout<<"List is empty "<<endl; return;
}
else if(start->link==NULL)
{
node *p= start;
start= NULL;
delete p;
}
else
{
node *p,*t;
p= start;
while(p->link!= NULL)
{
t=p;
p=p->link;
}
t->link= NULL;
delete p;
}
}

node * getMaxNode(){
node *t,*p;

if(start==NULL){ return NULL;}
else
{
p= start;
int mx= p->data; t= p;
while(p!= NULL)
{
if(mx<p->data )
{
mx= p->data;
t= p;
}

p= p->link;
}

}
return t;
}

void reverseList()
{
node * pre;
node * curr;
node * next;
pre = NULL;
curr= start;
next = NULL;
while (curr!=NULL)

{
next= curr->link;
curr->link= pre;
pre = curr;
curr= next;

} start= pre;

return;
}
int main()
{
insertLast(11);
insertBegin(20);
insertBegin(25);
insertBegin(30);

traverse();
/*searchItem(25);
searchItem(40);
insertLast(22);
traverse();
insertAfter(20,70);
traverse();
deleteBegin();
traverse();
deleteLast();
traverse();*/
reverseList();
traverse();
}