Skip to content

inserted on more node #103

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

Closed
wants to merge 4 commits into from
Closed
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
30 changes: 20 additions & 10 deletions linked_list_problems/deleteNode.cpp
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ struct Node {
int data;
Node * next;
Node( int d ) : data{ d }, next{ nullptr } { }
};
}*head=NULL;

void insert( Node * & head, int data )
{
@@ -32,17 +32,26 @@ void printList( Node * head )
std::cout << "NULL" << std::endl;
}

void deleteNode( Node * node )
void deleteNode( Node * node ) // works in every condition
{
// return if node is null
if ( node == nullptr ) {
return;
Node *p=head;
Node *q=NULL;
while(p!=node)
{
q=p;
p=p->next;
}
q->next=p->next;
p->next=NULL;
delete p;





//this method won't work if we are given last node
if ( node->next == nullptr ) {
return;
}


}

Node * nextNode = node->next;
node->data = nextNode->data;
@@ -52,12 +61,13 @@ void deleteNode( Node * node )

int main()
{
Node * head = nullptr;

insert( head, 1 );
insert( head, 12 );
insert( head, 2 );
insert( head, 3 );
insert( head, 4 );
insert( head, 5 );
printList( head );
std::cout << "Deleting node with data 12 \n";
deleteNode( head->next );
18 changes: 10 additions & 8 deletions linked_list_problems/reverseLinkedListIterAndRecurse.cpp
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ struct Node {
int data;
Node * next;
Node( int d ) : data{ d }, next{ nullptr } { }
};
}*head=NULL;

void push( Node * & head, int data ) {
Node *newNode = new Node(data);
@@ -48,16 +48,18 @@ void reverseRecur( Node * & head ) {
if ( head == nullptr || ( head != nullptr && head->next == nullptr ) ) {
return;
}
Node * first = head;
Node * rest = head->next;
reverseRecur( rest );
first->next->next = first;
first->next = nullptr;
head = rest;
else
{
std :: cout<<head->data;
reverseRecur(head->next);
}
}




int main() {
Node *head = nullptr;

push( head, 1 );
push( head, 2 );
push( head, 3 );
2 changes: 1 addition & 1 deletion sort_search_problems/findMaximum.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/** this is valid on both increasing or decreasing
* Find the maximum element in an array which is first increasing and then decreasing.
* Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1}
* Output: 500