Skip to content

For students in my lab for Programming 2. Warm up for learning how to contribute to github and improve understanding of linked list.

Notifications You must be signed in to change notification settings

ElissaBD/SinglyLinkedListWarmUp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

#include

class Node { public: int data; Node* next;

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

};

class SinglyLinkedList { private: Node* head;

public: SinglyLinkedList() : head(nullptr) {}

void append(int val) {
    Node* newNode = new Node(val);
    if (!head) {
        head = newNode;
        return;
    }
    Node* temp = head;
    while (temp->next) temp = temp->next;
    temp->next = newNode;
}

void printList() {
    Node* temp = head;
    while (temp) {
        std::cout << temp->data << " -> ";
        temp = temp->next;
    }
    std::cout << "NULL" << std::endl;
}

void reverseLinkedList() {
    Node* prev = nullptr;
    Node* current = head;
    Node* next = nullptr;

    while (current != nullptr) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }

    head = prev;
}

};

int main() { SinglyLinkedList list; list.append(1); list.append(2); list.append(3); list.printList();

list.reverseLinkedList();
list.printList();

return 0;

}

About

For students in my lab for Programming 2. Warm up for learning how to contribute to github and improve understanding of linked list.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%