{"id":15049025,"url":"https://github.com/gyakobo/singly-linked-list","last_synced_at":"2026-02-10T21:01:56.404Z","repository":{"id":247043727,"uuid":"823502104","full_name":"Gyakobo/Singly-Linked-List","owner":"Gyakobo","description":"This project showcases and more importantly explains a simple example of Singly Linked List.","archived":false,"fork":false,"pushed_at":"2024-07-06T07:59:56.000Z","size":26,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T23:44:24.773Z","etag":null,"topics":["c-programming-language","python","queue","singly-linked-list"],"latest_commit_sha":null,"homepage":"https://en.wikipedia.org/wiki/Linked_list","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Gyakobo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-03T06:49:12.000Z","updated_at":"2024-12-03T18:10:17.000Z","dependencies_parsed_at":"2024-07-06T09:09:57.040Z","dependency_job_id":null,"html_url":"https://github.com/Gyakobo/Singly-Linked-List","commit_stats":null,"previous_names":["gyakobo/singly-linked-list"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Gyakobo/Singly-Linked-List","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSingly-Linked-List","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSingly-Linked-List/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSingly-Linked-List/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSingly-Linked-List/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gyakobo","download_url":"https://codeload.github.com/Gyakobo/Singly-Linked-List/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSingly-Linked-List/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272977638,"owners_count":25025169,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-31T02:00:09.071Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-programming-language","python","queue","singly-linked-list"],"created_at":"2024-09-24T21:17:40.870Z","updated_at":"2026-02-10T21:01:51.303Z","avatar_url":"https://github.com/Gyakobo.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Singly Linked List\r\n\r\n![image](https://img.shields.io/badge/Python-FFD43B?style=for-the-badge\u0026logo=python\u0026logoColor=blue)\r\n![image](https://img.shields.io/badge/C-00599C?style=for-the-badge\u0026logo=c\u0026logoColor=white)\r\n![image](https://img.shields.io/badge/C%2B%2B-00599C?style=for-the-badge\u0026logo=c%2B%2B\u0026logoColor=white)\r\n![image](https://img.shields.io/badge/CMake-064F8C?style=for-the-badge\u0026logo=cmake\u0026logoColor=white)\r\n![image](https://img.shields.io/badge/windows%20terminal-4D4D4D?style=for-the-badge\u0026logo=windows%20terminal\u0026logoColor=white)\r\n\r\nAuthor: [Andrew Gyakobo](https://github.com/Gyakobo)\r\n\r\n\u003e[!NOTE]\r\n\u003eJust as a  small personal note and a small gag per say; this program is written in clean C and has no errors or warnings. The program was run and precompiled into an `.exe` with the following bash command:\r\n\r\n```bash\r\n$ sudo gcc -ansi -Wpedantic -Wextra -Wall main.c -o exe\r\n```\r\n\r\nThis project showcases and more importantly explains a simple example of Singly Linked List.\r\n\r\n## Introduction\r\n\r\n**A Singly Linked List** is a linear data structure where each element, called a node, contains two parts:\r\n\r\n1. Data: The value stored in the node\r\n1. Pointer (or Reference): A reference to then next node in the sequence\r\n\r\nThe first node in the list is called a head, and the last node in the list has a reference to `null`, indicating the end of the list.\r\n\r\n## Methodology\r\n\r\nAs of the current I wrote 2 programs for you, my dear explorer, to test. One in [python](https://github.com/Gyakobo/Singly-Linked-List/blob/main/main.py) and the other in the [C programming language](https://github.com/Gyakobo/Singly-Linked-List/blob/main/main.c)\r\n\r\nNeedless to say, each link in this singly linked list is going to be represented by a node, followed by the `class SinglyLinkedList` or `struct List` implementation:\r\n\r\nC code\r\n```c\r\n#include \u003cstdio.h\u003e\r\n#include \u003cstdlib.h\u003e\r\n\r\nstruct Node {\r\n    int item;\r\n    struct Node * next;\r\n};\r\n\r\nstruct List {\r\n    struct Node * head;\r\n    struct Node * tail;\r\n};\r\n\r\nstruct List new_list() {\r\n    struct List list = {NULL, NULL};\r\n    return list;\r\n}\r\n```\r\n\r\nPython\r\n```python\r\nclass Node:\r\n    def __init__(self, data):\r\n        self.data = data\r\n        self.next = None\r\n\r\nclass SinglyLinkedList:\r\n    def __init__(self):\r\n        self.head = None\r\n```\r\n\r\nBefore we delve into the code let's first understand the basic operations. \r\n\r\n1. **Insertion**\r\n\r\n    * *At the beginning (head)*: This operation involves creating a new node and updating the head pointer to this new node.\r\n\r\n        * Time Complexity: $O(1)$\r\n    \r\n    * *At the end*: This operation requires traversal to the end of the list to add the new node.\r\n\r\n        * Time Complexity: $O(n)$, where n is the number of nodes in the list. \r\n\r\n    * *At a specific position*: This operation involves traversing to the desired position and inserting the new node.\r\n\r\n        * Time Complexity: $O(n)$ in the worst case.\r\n\r\nC code\r\n```c\r\nvoid SLL_push(struct List * list, int item) {\r\n    struct Node * p = malloc(sizeof(struct Node));\r\n    p-\u003eitem = item;\r\n\r\n    if (SLL_empty(list)) {\r\n        list-\u003ehead = p;        \r\n        list-\u003etail = p;        \r\n    }\r\n\r\n    else {\r\n        p-\u003enext     = list-\u003ehead;\r\n        list-\u003ehead  = p; \r\n    }\r\n}\r\n\r\nvoid SLL_append(struct List * list, int item) {\r\n    struct Node * p = malloc(sizeof(struct Node));\r\n    p-\u003eitem = item;\r\n\r\n    if (SLL_empty(list)) SLL_push(list, item);\r\n    else {\r\n        list-\u003etail-\u003enext = p;\r\n        list-\u003etail = p;\r\n    }\r\n}\r\n```\r\n\r\nPython\r\n```python    \r\ndef insert_at_beginning(self, data):\r\n        new_node = Node(data)\r\n        new_node.next = self.head\r\n        self.head = new_node\r\n\r\ndef insert_at_end(self, data):\r\n    new_node = Node(data)\r\n    if not self.head:\r\n        self.head = new_node\r\n        return\r\n    last_node = self.head\r\n    while last_node.next:\r\n        last_node = last_node.next\r\n    last_node.next = new_node\r\n```\r\n\r\n2. **Deletion**\r\n\r\n    * *At the beginning(head)*: This operation involves updating the head pointer to the next node.\r\n        \r\n        * Time complexity: $O(1)$\r\n    \r\n    * *At the end*: This operation requires traversal to the second last node to update its reference to `null`.\r\n\r\n        * Time complexity: $O(n)$\r\n\r\n    * *At a specific position*: This operation involves traversal to the node just before the one to be deleted and updating its reference.\r\n        \r\n        * Time complexity: $O(n)$\r\n\r\nC code\r\n```c\r\nint SLL_pop(struct List * list){\r\n    struct Node * p;\r\n    int item;\r\n\r\n    if (!SLL_empty(list)) {\r\n        p = list-\u003ehead;\r\n        item = p-\u003eitem;\r\n\r\n        list-\u003ehead = p-\u003enext; \r\n        free(p);\r\n        return item;\r\n    }\r\n\r\n    return -1; /* Gotto change */\r\n}\r\n```\r\n\r\nPython\r\n```python\r\ndef delete_node(self, key):\r\n       temp = self.head\r\n\r\n       if temp is not None:\r\n           if temp.data == key:\r\n               self.head = temp.next\r\n               temp = None\r\n               return\r\n\r\n       while temp is not None:\r\n           if temp.data == key:\r\n               break\r\n           prev = temp\r\n           temp = temp.next\r\n\r\n       if temp == None:\r\n           return\r\n\r\n       prev.next = temp.next\r\n       temp = None\r\n```\r\n\r\n3. **Search**\r\n    * Searching for an element involves traversing the list and comparing each node's data with the target value.\r\n        * Time Complexity: $O(n)$\r\n\r\nPython\r\n```python\r\ndef search(self, key):\r\n       current = self.head\r\n       while current:\r\n           if current.data == key:\r\n               return True\r\n           current = current.next\r\n       return False\r\n```\r\n\r\n## Space Complexity \r\n\r\nThe space complexity for a singly linked list is $O(n)$ because each node requires space for the data and the reference to the next node.\r\n\r\n### Advantages of Singly Linked Lists\r\n\r\n1. *Dynamic Size*: They can easily grow and shrink in size by allocating and deallocating memory as needed.\r\n\r\n1. *Efficient Insertions/Deletions*: Insertions and deletions (especially at the beginning) are more efficient compared to arrays since no shifting of elements is required.\r\n\r\n### Disadvantages of Singly Linked Lists\r\n\r\n1. *No Direct Access*: Accessing an element by index requires traversal from the head, which can be time-consuming.\r\n\r\n1. *Memory Overhead*: Additional memory is required for storing the reference to the next node for each element.\r\n\r\n1. *Poor Cache Performance*: Due to non-contiguous memory allocation, linked lists may suffer from poor cache performance compared to arrays.\r\n\r\n## License\r\nMIT\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyakobo%2Fsingly-linked-list","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgyakobo%2Fsingly-linked-list","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyakobo%2Fsingly-linked-list/lists"}