{"id":2310,"date":"2026-07-20T12:41:36","date_gmt":"2026-07-20T10:41:36","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/everything-about-linked-list-with-python-code-and-hackerrank-leetcode-solutions\/"},"modified":"2026-08-26T21:16:10","modified_gmt":"2026-08-26T19:16:10","slug":"everything-about-linked-list-with-python-code-and-hackerrank-leetcode-solutions","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/everything-about-linked-list-with-python-code-and-hackerrank-leetcode-solutions\/","title":{"rendered":"Everything About Linked List With Python Code and HackerRank\/LeetCode Solutions"},"content":{"rendered":"<p><!-- ktg-updated-banner --><\/p>\n<p><em>Updated August 2026 \u2014 full tutorial restored for this URL.<\/em><\/p>\n<p>This is a practical <strong>linked list<\/strong> guide in <strong>Python<\/strong>: structure, core operations, and patterns that show up on HackerRank \/ LeetCode. Also see <a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-reverse-a-linked-list\/\">How to Reverse a Linked List<\/a>.<\/p>\n<ol>\n<li><a href=\"#t1\">Singly linked list basics<\/a><\/li>\n<li><a href=\"#t2\">Insert and delete<\/a><\/li>\n<li><a href=\"#t3\">Traverse and reverse<\/a><\/li>\n<li><a href=\"#t4\">Two-pointer patterns (middle, cycle)<\/a><\/li>\n<li><a href=\"#t5\">Merge two sorted lists<\/a><\/li>\n<li><a href=\"#t6\">Practice checklist<\/a><\/li>\n<\/ol>\n<p><strong id=\"t1\">1. Singly linked list basics<\/strong><\/p>\n<pre><code>class Node:\n    def __init__(self, val, next=None):\n        self.val = val\n        self.next = next\n\nclass LinkedList:\n    def __init__(self):\n        self.head = None\n<\/code><\/pre>\n<p>Arrays give O(1) index access; linked lists give O(1) insert\/delete at a known node (after you find it).<\/p>\n<p><strong id=\"t2\">2. Insert and delete<\/strong><\/p>\n<pre><code>def push_front(self, val):\n    self.head = Node(val, self.head)\n\ndef delete_val(self, val):\n    dummy = Node(0, self.head)\n    cur = dummy\n    while cur.next:\n        if cur.next.val == val:\n            cur.next = cur.next.next\n            break\n        cur = cur.next\n    self.head = dummy.next\n<\/code><\/pre>\n<p>Dummy nodes simplify edge cases at the head.<\/p>\n<p><strong id=\"t3\">3. Traverse and reverse<\/strong><\/p>\n<pre><code>def to_list(head):\n    out = []\n    while head:\n        out.append(head.val)\n        head = head.next\n    return out\n\ndef reverse(head):\n    prev = None\n    while head:\n        nxt = head.next\n        head.next = prev\n        prev, head = head, nxt\n    return prev\n<\/code><\/pre>\n<p><strong id=\"t4\">4. Two-pointer patterns (middle, cycle)<\/strong><\/p>\n<pre><code>def middle(head):\n    slow = fast = head\n    while fast and fast.next:\n        slow = slow.next\n        fast = fast.next.next\n    return slow\n\ndef has_cycle(head):\n    slow = fast = head\n    while fast and fast.next:\n        slow = slow.next\n        fast = fast.next.next\n        if slow is fast:\n            return True\n    return False\n<\/code><\/pre>\n<p>Floyd\u2019s cycle detection is a classic interview question (LeetCode 141).<\/p>\n<p><strong id=\"t5\">5. Merge two sorted lists<\/strong><\/p>\n<pre><code>def merge(l1, l2):\n    dummy = Node(0)\n    cur = dummy\n    while l1 and l2:\n        if l1.val &lt;= l2.val:\n            cur.next, l1 = l1, l1.next\n        else:\n            cur.next, l2 = l2, l2.next\n        cur = cur.next\n    cur.next = l1 or l2\n    return dummy.next\n<\/code><\/pre>\n<p><strong id=\"t6\">6. Practice checklist<\/strong><\/p>\n<ul>\n<li>Reverse list \/ reverse in k-groups<\/li>\n<li>Remove nth from end (two pointers)<\/li>\n<li>Detect\/start of cycle<\/li>\n<li>Merge k sorted lists (heap)<\/li>\n<li>Copy list with random pointer<\/li>\n<\/ul>\n<p>Draw pointers on paper before coding \u2014 most bugs are lost references, not syntax.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updated August 2026 \u2014 full tutorial restored for this URL. This is a practical linked list guide in Python: structure, core operations, and patterns that &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[35],"tags":[],"class_list":["post-2310","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"acf":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=2310"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2310\/revisions"}],"predecessor-version":[{"id":2458,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2310\/revisions\/2458"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=2310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=2310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=2310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}