Skip to content

Commit

Permalink
Added reverseLL
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshat162001 committed Oct 31, 2023
1 parent feb83a8 commit 0c41ba6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
10 changes: 6 additions & 4 deletions C++/Added Set Matrix Zeroes → C++/Added Set Matrix Zeroes.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
//LEETCODE QUES-73

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void setZeroes(vector<vector<int>> &matrix)
{
set<int> s,ss;
set<int> ss;
for (int i = 0; i < matrix.size(); i++)
{
for (int j = 0; j < matrix[0].size(); j++)
{
if (matrix[i][j] == 0)
{
s.insert(i);
ss.insert(i);
ss.insert(j);
}
}
}
vector<int>vec,tec;
set<int>::iterator itr;
for (itr = s.begin();
itr != s.end(); itr++)
for (itr = ss.begin();
itr != ss.end(); itr++)
{
vec.push_back(*itr);
}
Expand Down
File renamed without changes.
50 changes: 50 additions & 0 deletions C++/reverseLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>

struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};

// Function to reverse a singly linked list
Node* reverseLinkedList(Node* head) {
Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;

while (current != nullptr) {
next = current->next; // Store the next node
current->next = prev; // Change the current node's next to the previous node
prev = current; // Move the previous pointer to the current node
current = next; // Move the current pointer to the next node
}

return prev; // New head of the reversed list
}

// Function to print a linked list
void printLinkedList(Node* head) {
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "nullptr" << std::endl;
}

int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);

std::cout << "Original linked list: ";
printLinkedList(head);

head = reverseLinkedList(head);

std::cout << "Reversed linked list: ";
printLinkedList(head);

return 0;
}

0 comments on commit 0c41ba6

Please sign in to comment.