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

Reverse a Linked list #50

Closed
Closed
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
52 changes: 52 additions & 0 deletions Arrays/move_zeros_to_last.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Question: Given an array of random numbers move all zeros of array to the end
Approach/Algo:
Step 1: Iterate through the array from left to right and maintain count of non-zero elements
Step 2: The element other than 0 i.e arr[i], put the element at arr[count] and increment count
Step 3: The count will be at first 0 element, just iterate a loop to end and make element zero.
*/


#include <iostream>
using namespace std;

void pushZerosToEnd(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if (arr[i] != 0)
arr[count++] = arr[i];

for(int j=count;j<n;j++)
arr[count++] = 0;
}


int main()
{
int arr[] = {1, 2, 0, 4, 3, 0, 5, 0};
int n = sizeof(arr) / sizeof(arr[0]);
pushZerosToEnd(arr, n);
cout << "Array after zeros pushed to end:\n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}

/*
Test Cases:

1.
Input: arr = {1, 2, 0, 4, 3, 0, 5, 0};
Output: arr = {1, 2, 4, 3, 5, 0, 0, 0};


2.
Input: arr = {2, 1, 4, 0, 1, 7, 0, 5, 0}
Output: arr = {2, 1, 4, 1, 7, 5, 0, 0, 0}

*/
/*
Time complexity: O(n) --> (Since looping through the array of n element)
Space complexity: O(1)
*/
114 changes: 114 additions & 0 deletions Linked List/reverse_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Question: Reverse a given linked list

Approach/Algo:
Step 1: Intialize three pointers, with the following values:
prev --> NULL
curr --> HEAD
next --> NULL

Step 2: Iterate through the linked list and make the following changes:
1. Store next node, before changing the value of next of current (next = curr -> next)
2. Change next to curr, (curr -> next = prev)
3. Lastly, move prev and curr one step ahead ( prev = curr, curr = next)
*/


#include <iostream>
using namespace std;

struct Node {
int data;
struct Node* next;
Node(int data)
{
this->data = data;
next = NULL;
}
};

struct LinkedList {
Node* head;
LinkedList() { head = NULL; }


void reverseLinkedList()
{

// Step-1
Node* current = head;
Node *prev = NULL, *next = NULL;

// Performing Step-2.
while (current != NULL) {
next = current->next;

current->next = prev;

prev = current;
current = next;
}
head = prev;
}


void print()
{
struct Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}

void push(int data)
{
Node* temp = new Node(data);
temp->next = head;
head = temp;
}
};

int main()
{

LinkedList ll;
//{Pushing element in linked list}
ll.push(13);
ll.push(5);
ll.push(12);
ll.push(3);

cout << "Linked list"<<endl;
ll.print();

ll.reverseLinkedList();
cout<<endl;

cout << "Reversed Linked list "<<endl;
ll.print();
return 0;
}

/*
Test Case 1:
Input:
Linked List
1 -> 2 -> 3 -> 4 -> 5 -> NULL

Output:
Reversed values of Linked list
5 -> 4 -> 3 -> 2 -> 1 -> NULL

Test Case 2:
Input:
Linked List
3 -> 12 -> 4 -> NULL

Output:
Reversed values of Linked list
4 -> 12 -> 3 -> NULL

Time Complexity: O(N) -->Since Looping through the linked list till NULL is achived
Space Complexity: O(1)
*/