{"id":842,"date":"2026-10-10T09:00:00","date_gmt":"2026-10-10T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/"},"modified":"2026-11-29T01:29:26","modified_gmt":"2026-11-28T19:59:26","slug":"go-linked-list-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/","title":{"rendered":"Go Linked List Programs: Reverse, Middle, Cycle and Merge"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go linked list programs are one of the most asked topics in coding interviews, because they test pointers, loops and careful thinking all at once. In this post, you will write fourteen programs with their exact output and the logic explained in comments, covering insert, delete, search, reverse, middle node, cycle check, merge and a generic list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should first finish our <a href=\"https:\/\/smitabharat.com\/blog\/go-struct-programs\/\">Go struct programs<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/\">Go pointer programs<\/a>, because every node is a struct linked by a pointer. The <a href=\"https:\/\/smitabharat.com\/blog\/go-generics-programs\/\">Go generics programs<\/a> help with the last program. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Linked List Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go linked list programs build a chain of nodes. Each node holds a value and a pointer to the next node. The first node is called the head, and the last node points to <code>nil<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a treasure hunt. Each clue tells you where the next clue is hidden. You cannot jump to clue five, so you follow the clues one by one from the start. A linked list works the same way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Linked List Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Slices are the normal choice in Go, so you may wonder why lists matter. There are four good reasons.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Interviews:<\/strong> reverse a list, find the middle and detect a cycle are asked in almost every round.<\/li>\n\n\n<li><strong>Fast insert and delete:<\/strong> adding a node at the front takes the same time for 10 or 10 million nodes.<\/li>\n\n\n<li><strong>Real systems:<\/strong> LRU caches, undo history and task queues often use linked lists inside.<\/li>\n\n\n<li><strong>Pointer practice:<\/strong> these programs make you really understand how pointers work.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Linked List Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every program in this post uses the same <code>Node<\/code> type. A node has a <code>Value<\/code> field and a <code>Next<\/code> field that points to another node. To walk the list, you start at the head and follow <code>Next<\/code> until you reach <code>nil<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-1-1-1024x640.png\" alt=\"Go linked list programs diagram showing head node, next pointers and nil at the end\" class=\"wp-image-863\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-1-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-1-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-1-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-1-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-1-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Each node stores a value and a pointer to the next node, and the last node points to nil.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows the cost of common list operations.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Operation<\/th><th>Linked list<\/th><th>Slice<\/th><\/tr><\/thead><tbody><tr><td>Read item at index i<\/td><td>O(n), walk from head<\/td><td>O(1)<\/td><\/tr><tr><td>Insert at the front<\/td><td>O(1)<\/td><td>O(n), shifts items<\/td><\/tr><tr><td>Insert at the end<\/td><td>O(n), or O(1) with a tail pointer<\/td><td>O(1) on average<\/td><\/tr><tr><td>Delete a known node<\/td><td>O(1) after you find it<\/td><td>O(n)<\/td><\/tr><tr><td>Memory per item<\/td><td>Value plus a pointer<\/td><td>Value only<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go Linked List Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go linked list programs build a list, print it, add nodes, remove nodes and search for a value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Link Three Nodes by Hand<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one box in the chain. It holds a number and a link to the next box.\ntype Node struct {\n    Value int   \/\/ the number stored in this node\n    Next  *Node \/\/ *Node means \"address of a Node\"; nil means no next node\n}\n\nfunc main() {\n    \/\/ Step 1: make three separate nodes.\n    first := &amp;Node{Value: 10} \/\/ := creates a variable; &amp; gives the address of the new node\n    second := &amp;Node{Value: 20}\n    third := &amp;Node{Value: 30}\n\n    \/\/ Step 2: link them into a chain.\n    first.Next = second\n    second.Next = third \/\/ third.Next stays nil, so the chain ends here\n\n    \/\/ Step 3: read the values by following the links.\n    fmt.Println(first.Value)\n    fmt.Println(first.Next.Value)      \/\/ one hop: first -&gt; second\n    fmt.Println(first.Next.Next.Value) \/\/ two hops: first -&gt; second -&gt; third\n    fmt.Println(third.Next == nil)     \/\/ nil means \"points to nothing\"\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n20\n30\ntrue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the smallest possible linked list. First you create three nodes, and then you set each <code>Next<\/code> field to the following node. To reach the third value, you start at <code>first<\/code> and follow <code>Next<\/code> twice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Build and Print a Linked List<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node \/\/ pointer to the next node, nil at the end\n}\n\n\/\/ printList walks from the head to nil and prints each value.\nfunc printList(head *Node) {\n    current := head \/\/ start at the first node\n    for current != nil {\n        fmt.Print(current.Value, \" -&gt; \")\n        current = current.Next \/\/ move one node forward\n    }\n    fmt.Println(\"nil\")\n}\n\n\/\/ fromSlice builds a list in the same order as the slice.\nfunc fromSlice(values []int) *Node {\n    var head *Node \/\/ first node, nil while the list is empty\n    var tail *Node \/\/ last node, so we can add after it quickly\n    for _, value := range values { \/\/ range gives each item; _ skips the index\n        newNode := &amp;Node{Value: value}\n        if head == nil {\n            head = newNode \/\/ the first node becomes the head\n        } else {\n            tail.Next = newNode \/\/ link the old tail to the new node\n        }\n        tail = newNode \/\/ the new node is now the last one\n    }\n    return head\n}\n\nfunc main() {\n    head := fromSlice([]int{10, 20, 30})\n    printList(head)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>10 -&gt; 20 -&gt; 30 -&gt; nil<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>fromSlice<\/code> function keeps a <code>tail<\/code> pointer while it builds, so each new node is linked in one step. Without it, you would walk the whole list for every insert. Then <code>printList<\/code> moves <code>current<\/code> forward until it becomes <code>nil<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Insert at the Head, Tail and a Position<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ printList prints every value from the head to the end.\nfunc printList(head *Node) {\n    current := head\n    for current != nil {\n        fmt.Print(current.Value, \" -&gt; \")\n        current = current.Next\n    }\n    fmt.Println(\"nil\")\n}\n\n\/\/ pushFront adds a node before the head and returns the new head.\nfunc pushFront(head *Node, value int) *Node {\n    return &amp;Node{Value: value, Next: head} \/\/ the new node points to the old head\n}\n\n\/\/ pushBack walks to the last node and links a new one after it.\nfunc pushBack(head *Node, value int) *Node {\n    newNode := &amp;Node{Value: value}\n    if head == nil {\n        return newNode \/\/ empty list: the new node is the whole list\n    }\n    current := head\n    for current.Next != nil {\n        current = current.Next \/\/ stop at the last node\n    }\n    current.Next = newNode\n    return head\n}\n\n\/\/ insertAt puts value at the given position (0 means the front).\nfunc insertAt(head *Node, position int, value int) *Node {\n    if position == 0 {\n        return pushFront(head, value)\n    }\n    \/\/ Step 1: walk to the node just before the position.\n    current := head\n    for i := 0; i &lt; position-1 &amp;&amp; current != nil; i++ {\n        current = current.Next\n    }\n    if current == nil {\n        return head \/\/ the position is past the end, so do nothing\n    }\n    \/\/ Step 2: the new node points to whatever came after current.\n    newNode := &amp;Node{Value: value, Next: current.Next}\n    \/\/ Step 3: current now points to the new node.\n    current.Next = newNode\n    return head\n}\n\nfunc main() {\n    var head *Node \/\/ an empty list\n    head = pushBack(head, 2)\n    head = pushBack(head, 4)\n    head = pushFront(head, 1)\n    head = insertAt(head, 2, 3)\n    printList(head)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; nil<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each function returns the head, because adding at the front changes which node is first. Always assign the result back, as in <code>head = pushFront(head, 1)<\/code>. For <code>insertAt<\/code>, you stop one node early, link the new node to the rest, and then link the earlier node to it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Delete a Node by Value<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ printList prints every value from the head to the end.\nfunc printList(head *Node) {\n    current := head\n    for current != nil {\n        fmt.Print(current.Value, \" -&gt; \")\n        current = current.Next\n    }\n    fmt.Println(\"nil\")\n}\n\n\/\/ deleteValue removes the first node that holds target.\nfunc deleteValue(head *Node, target int) *Node {\n    dummy := &amp;Node{Next: head} \/\/ a fake node before the head keeps the logic simple\n    previous := dummy\n    for previous.Next != nil {\n        if previous.Next.Value == target {\n            previous.Next = previous.Next.Next \/\/ skip over the node to remove it\n            break                              \/\/ stop after the first match\n        }\n        previous = previous.Next\n    }\n    return dummy.Next \/\/ the real head, which may have changed\n}\n\nfunc main() {\n    head := &amp;Node{1, &amp;Node{2, &amp;Node{3, nil}}} \/\/ builds 1 -&gt; 2 -&gt; 3 by nesting nodes\n    head = deleteValue(head, 2)\n    printList(head)\n    head = deleteValue(head, 1) \/\/ deleting the head works the same way\n    printList(head)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 -&gt; 3 -&gt; nil\n3 -&gt; nil<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop always looks one node ahead with <code>previous.Next<\/code>. When it finds the target, it links <code>previous<\/code> straight to the node after the target. The dummy node trick means you never need a special case for deleting the head. Interviewers like to see it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Search a Linked List for a Value<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ findIndex returns the position of target, or -1 if it is not in the list.\nfunc findIndex(head *Node, target int) int {\n    index := 0\n    current := head\n    for current != nil {\n        if current.Value == target {\n            return index \/\/ found it, so stop here\n        }\n        current = current.Next\n        index++\n    }\n    return -1 \/\/ we reached nil without finding the value\n}\n\nfunc main() {\n    head := &amp;Node{10, &amp;Node{20, &amp;Node{30, &amp;Node{40, nil}}}}\n    fmt.Println(\"index of 30:\", findIndex(head, 30))\n    fmt.Println(\"index of 99:\", findIndex(head, 99))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>index of 30: 2\nindex of 99: -1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You walk the list one node at a time and count the steps in <code>index<\/code>. If a node holds the target, you return the count right away. If you reach <code>nil<\/code> first, the value is missing, so you return -1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Go Linked List Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go linked list programs are the ones you will meet again and again in coding rounds.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Reverse a Linked List<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ printList prints every value from the head to the end.\nfunc printList(head *Node) {\n    current := head\n    for current != nil {\n        fmt.Print(current.Value, \" -&gt; \")\n        current = current.Next\n    }\n    fmt.Println(\"nil\")\n}\n\n\/\/ reverse flips every Next pointer using three pointers: prev, cur and next.\nfunc reverse(head *Node) *Node {\n    var prev *Node \/\/ the reversed part starts empty\n    cur := head    \/\/ the node we are working on now\n    for cur != nil {\n        next := cur.Next \/\/ Step 1: remember the rest of the list\n        cur.Next = prev  \/\/ Step 2: point this node backwards\n        prev = cur       \/\/ Step 3: move prev forward\n        cur = next       \/\/ Step 4: move cur forward\n    }\n    return prev \/\/ prev is the old last node, now the head\n}\n\nfunc main() {\n    head := &amp;Node{1, &amp;Node{2, &amp;Node{3, &amp;Node{4, nil}}}}\n    printList(reverse(head))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>4 -&gt; 3 -&gt; 2 -&gt; 1 -&gt; nil<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In Go linked list programs, this is the question asked most. You save <code>next<\/code> first, because changing <code>cur.Next<\/code> would lose the rest of the list. The four steps run once per node, so this is O(n) time and O(1) extra memory. Draw the boxes on paper the first few times.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-2-1024x640.png\" alt=\"Go linked list programs reverse steps with prev, cur and next pointers\" class=\"wp-image-841\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Each step saves next, turns cur.Next back to prev, then moves both pointers one node forward.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Find the Middle Node with Slow and Fast Pointers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ fromSlice builds a list in the same order as the slice.\nfunc fromSlice(values []int) *Node {\n    dummy := &amp;Node{} \/\/ a fake first node, so we never check for an empty list\n    tail := dummy\n    for _, value := range values {\n        tail.Next = &amp;Node{Value: value}\n        tail = tail.Next\n    }\n    return dummy.Next \/\/ skip the fake node\n}\n\n\/\/ middle moves slow one step and fast two steps.\n\/\/ When fast reaches the end, slow is in the middle.\nfunc middle(head *Node) *Node {\n    slow := head\n    fast := head\n    for fast != nil &amp;&amp; fast.Next != nil {\n        slow = slow.Next      \/\/ one step\n        fast = fast.Next.Next \/\/ two steps\n    }\n    return slow\n}\n\nfunc main() {\n    oddList := fromSlice([]int{1, 2, 3, 4, 5})\n    evenList := fromSlice([]int{1, 2, 3, 4, 5, 6})\n    fmt.Println(middle(oddList).Value)\n    fmt.Println(middle(evenList).Value) \/\/ even length: second middle\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3\n4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>fast<\/code> pointer moves twice as quickly as <code>slow<\/code>. So when <code>fast<\/code> reaches the end, <code>slow<\/code> has covered half the list. This finds the middle in one pass, without counting the length first. For an even length, it returns the second of the two middle nodes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Detect a Cycle (Floyd&#8217;s Method)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ hasCycle reports true if fast ever meets slow, which means the list loops.\nfunc hasCycle(head *Node) bool {\n    slow := head\n    fast := head\n    for fast != nil &amp;&amp; fast.Next != nil {\n        slow = slow.Next\n        fast = fast.Next.Next\n        if slow == fast { \/\/ compare pointers (same node), not values\n            return true\n        }\n    }\n    return false \/\/ fast reached nil, so the list has an end\n}\n\nfunc main() {\n    first := &amp;Node{Value: 1}\n    second := &amp;Node{Value: 2}\n    third := &amp;Node{Value: 3}\n    first.Next = second\n    second.Next = third\n    fmt.Println(\"cycle:\", hasCycle(first))\n\n    third.Next = first \/\/ the last node now points back to the first\n    fmt.Println(\"cycle:\", hasCycle(first))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cycle: false\ncycle: true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the list has an end, <code>fast<\/code> reaches <code>nil<\/code> and the function returns false. Inside a loop, the fast pointer gains one node on the slow pointer each step, so it must catch up. This needs no extra map, so memory stays O(1).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Merge Two Sorted Lists<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ fromSlice builds a list in the same order as the slice.\nfunc fromSlice(values []int) *Node {\n    dummy := &amp;Node{}\n    tail := dummy\n    for _, value := range values {\n        tail.Next = &amp;Node{Value: value}\n        tail = tail.Next\n    }\n    return dummy.Next\n}\n\n\/\/ printList prints every value from the head to the end.\nfunc printList(head *Node) {\n    for current := head; current != nil; current = current.Next {\n        fmt.Print(current.Value, \" -&gt; \")\n    }\n    fmt.Println(\"nil\")\n}\n\n\/\/ merge picks the smaller front node each time and links it to the result.\nfunc merge(first, second *Node) *Node {\n    dummy := &amp;Node{}\n    tail := dummy \/\/ last node of the merged list\n    for first != nil &amp;&amp; second != nil {\n        if first.Value &lt;= second.Value {\n            tail.Next = first   \/\/ take the node from the first list\n            first = first.Next  \/\/ move along the first list\n        } else {\n            tail.Next = second  \/\/ take the node from the second list\n            second = second.Next\n        }\n        tail = tail.Next\n    }\n    if first != nil { \/\/ one list may still have nodes left\n        tail.Next = first\n    } else {\n        tail.Next = second\n    }\n    return dummy.Next\n}\n\nfunc main() {\n    listA := fromSlice([]int{1, 4, 7})\n    listB := fromSlice([]int{2, 3, 8, 9})\n    printList(merge(listA, listB))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 7 -&gt; 8 -&gt; 9 -&gt; nil<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each round compares the front of both lists and moves the smaller node to the result. When one list runs out, the other list is already sorted, so you attach it in one step. No new nodes are created, so the merge uses O(1) extra memory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Remove the Nth Node from the End<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ fromSlice builds a list in the same order as the slice.\nfunc fromSlice(values []int) *Node {\n    dummy := &amp;Node{}\n    tail := dummy\n    for _, value := range values {\n        tail.Next = &amp;Node{Value: value}\n        tail = tail.Next\n    }\n    return dummy.Next\n}\n\n\/\/ printList prints every value from the head to the end.\nfunc printList(head *Node) {\n    for current := head; current != nil; current = current.Next {\n        fmt.Print(current.Value, \" -&gt; \")\n    }\n    fmt.Println(\"nil\")\n}\n\n\/\/ removeFromEnd removes the node that is stepsFromEnd places from the end.\nfunc removeFromEnd(head *Node, stepsFromEnd int) *Node {\n    dummy := &amp;Node{Next: head}\n    fast := dummy\n    slow := dummy\n    \/\/ Step 1: move fast ahead to make a gap between the two pointers.\n    for i := 0; i &lt;= stepsFromEnd; i++ {\n        fast = fast.Next\n    }\n    \/\/ Step 2: move both pointers together until fast falls off the end.\n    for fast != nil {\n        fast = fast.Next\n        slow = slow.Next\n    }\n    \/\/ Step 3: slow is just before the target, so skip the target.\n    slow.Next = slow.Next.Next\n    return dummy.Next\n}\n\nfunc main() {\n    head := fromSlice([]int{1, 2, 3, 4, 5})\n    printList(removeFromEnd(head, 2))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 -&gt; 2 -&gt; 3 -&gt; 5 -&gt; nil<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>fast<\/code> moves ahead to open a fixed gap. Then both pointers move together, so the gap stays the same. When <code>fast<\/code> falls off the end, <code>slow<\/code> sits right before the node to remove. Again, the dummy node handles the case where the head itself is removed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Check if a List is a Palindrome<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ fromSlice builds a list in the same order as the slice.\nfunc fromSlice(values []int) *Node {\n    dummy := &amp;Node{}\n    tail := dummy\n    for _, value := range values {\n        tail.Next = &amp;Node{Value: value}\n        tail = tail.Next\n    }\n    return dummy.Next\n}\n\n\/\/ reverse flips the list one node at a time and returns the new head.\nfunc reverse(head *Node) *Node {\n    var previous *Node\n    current := head\n    for current != nil {\n        nextNode := current.Next \/\/ Step 1: save the rest of the list\n        current.Next = previous  \/\/ Step 2: point this node backwards\n        previous = current       \/\/ Step 3: move previous forward\n        current = nextNode       \/\/ Step 4: move current forward\n    }\n    return previous\n}\n\n\/\/ isPalindrome finds the middle, reverses the second half, then compares.\nfunc isPalindrome(head *Node) bool {\n    slow := head\n    fast := head\n    for fast != nil &amp;&amp; fast.Next != nil {\n        slow = slow.Next\n        fast = fast.Next.Next\n    }\n    firstHalf := head\n    secondHalf := reverse(slow) \/\/ slow is at the middle\n    for secondHalf != nil {\n        if firstHalf.Value != secondHalf.Value {\n            return false\n        }\n        firstHalf = firstHalf.Next\n        secondHalf = secondHalf.Next\n    }\n    return true\n}\n\nfunc main() {\n    fmt.Println(isPalindrome(fromSlice([]int{1, 2, 3, 2, 1})))\n    fmt.Println(isPalindrome(fromSlice([]int{1, 2, 3, 4})))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true\nfalse<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, slow and fast pointers find the middle. Next, you reverse the second half, so it reads from the end back to the middle. Finally, you compare both halves node by node. Combining small tricks like this is how most list questions are solved.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Other Kinds of Go Linked List Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So far every node pointed only forward. The next Go linked list programs add a backward pointer, use the standard library and make the list generic.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: A Doubly Linked List<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ DNode is a node that points both forward (Next) and backward (Prev).\ntype DNode struct {\n    Value string\n    Prev  *DNode\n    Next  *DNode\n}\n\n\/\/ DList keeps track of both ends of a doubly linked list.\ntype DList struct {\n    head *DNode\n    tail *DNode\n}\n\n\/\/ PushBack links a new node after the current tail.\nfunc (list *DList) PushBack(value string) {\n    newNode := &amp;DNode{Value: value, Prev: list.tail} \/\/ it points back to the old tail\n    if list.tail == nil {\n        list.head = newNode \/\/ empty list: the new node is head and tail\n    } else {\n        list.tail.Next = newNode\n    }\n    list.tail = newNode\n}\n\nfunc main() {\n    var history DList \/\/ the zero value is an empty list\n    for _, page := range []string{\"home\", \"search\", \"product\"} {\n        history.PushBack(page)\n    }\n    for node := history.head; node != nil; node = node.Next {\n        fmt.Print(node.Value, \" \")\n    }\n    fmt.Println()\n    for node := history.tail; node != nil; node = node.Prev { \/\/ walk backwards, like a Back button\n        fmt.Print(node.Value, \" \")\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>home search product\nproduct search home<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each new node points back to the old tail, and the old tail points forward to it. The first loop follows <code>Next<\/code> from the head. The second loop follows <code>Prev<\/code> from the tail, so the pages print in reverse. Browser history works like this.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Use container\/list from the Standard Library<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"container\/list\"\n    \"fmt\"\n)\n\nfunc main() {\n    letters := list.New() \/\/ a ready-made doubly linked list\n    letters.PushBack(\"b\")\n    letters.PushBack(\"c\")\n    front := letters.PushFront(\"a\") \/\/ returns the element, so we can use it later\n    letters.InsertAfter(\"a2\", front)\n\n    for element := letters.Front(); element != nil; element = element.Next() {\n        fmt.Print(element.Value, \" \")\n    }\n    fmt.Println(\"| len:\", letters.Len())\n\n    letters.Remove(front) \/\/ O(1) because we already hold the element\n    fmt.Println(\"front now:\", letters.Front().Value)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>a a2 b c | len: 4\nfront now: a2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here <code>list.New<\/code> gives you a doubly linked list, so you do not write any node code. You add items with <code>PushBack<\/code>, <code>PushFront<\/code> and <code>InsertAfter<\/code>, then walk it with <code>Front<\/code> and <code>Next<\/code>. <code>container\/list<\/code> stores values as <code>any<\/code>, so you need a type assertion like <code>element.Value.(string)<\/code> to get a string back.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: A Generic Linked List<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ node holds one value of any type T.\ntype node[T any] struct {\n    value T\n    next  *node[T]\n}\n\n\/\/ List[T] works for any type T, with a size counter.\ntype List[T any] struct {\n    head *node[T]\n    size int\n}\n\n\/\/ Push adds a value at the front in O(1).\nfunc (list *List[T]) Push(value T) {\n    list.head = &amp;node[T]{value: value, next: list.head}\n    list.size++\n}\n\n\/\/ Values returns the items from head to tail as a slice.\nfunc (list *List[T]) Values() []T {\n    result := make([]T, 0, list.size) \/\/ make creates an empty slice with room for size items\n    for current := list.head; current != nil; current = current.next {\n        result = append(result, current.value) \/\/ append adds to the end of the slice\n    }\n    return result\n}\n\nfunc main() {\n    var numbers List[int] \/\/ T is int here\n    numbers.Push(3)\n    numbers.Push(2)\n    numbers.Push(1)\n\n    var names List[string] \/\/ T is string here\n    names.Push(\"ravi\")\n    names.Push(\"asha\")\n\n    fmt.Println(numbers.Values(), numbers.size)\n    fmt.Println(names.Values(), names.size)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3] 3\n[asha ravi] 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>[T any]<\/code> part means the list can hold any type. You write the code once, then use <code>List[int]<\/code> and <code>List[string]<\/code> with full type safety. Keeping a <code>size<\/code> field makes the length O(1), so you never walk the list to count nodes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Linked List vs Slice in Go Linked List Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both store a sequence of items, but they are good at different things. The table below compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Linked list<\/th><th>Slice<\/th><\/tr><\/thead><tbody><tr><td>Memory layout<\/td><td>Nodes spread out in memory<\/td><td>One block of memory<\/td><\/tr><tr><td>Read by index<\/td><td>Slow, walk from head<\/td><td>Instant<\/td><\/tr><tr><td>Insert at front<\/td><td>Instant<\/td><td>Slow, shifts every item<\/td><\/tr><tr><td>CPU cache use<\/td><td>Poor<\/td><td>Very good<\/td><\/tr><tr><td>Extra memory<\/td><td>One or two pointers per node<\/td><td>Some spare capacity<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A simple rule helps. In real Go code, start with a slice. Use a linked list when you insert and remove in the middle often, or when an interviewer asks for one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Linked List Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Losing the rest of the list:<\/strong> save <code>next := cur.Next<\/code> before changing <code>cur.Next<\/code>.<\/li>\n\n\n<li><strong>Not returning the new head:<\/strong> functions that change the front must return it.<\/li>\n\n\n<li><strong>Nil pointer panics:<\/strong> check <code>fast != nil &amp;&amp; fast.Next != nil<\/code> before <code>fast.Next.Next<\/code>.<\/li>\n\n\n<li><strong>Comparing values instead of pointers:<\/strong> cycle checks must compare nodes, not <code>Value<\/code>.<\/li>\n\n\n<li><strong>Special-casing the head:<\/strong> use a dummy node to remove that extra code.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Linked List Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Draw boxes and arrows<\/strong> before writing code, and update them after each line.<\/li>\n\n\n<li><strong>Use a dummy node<\/strong> for insert, delete and merge.<\/li>\n\n\n<li><strong>Test small cases:<\/strong> an empty list, one node and two nodes.<\/li>\n\n\n<li><strong>Keep a tail pointer and size<\/strong> when you add at the end or need the length often.<\/li>\n\n\n<li><strong>Prefer slices in production code<\/strong> unless you have a clear reason for a list.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Linked List Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These are the list questions interviewers ask most, and they build on the Go linked list programs above. Each code answer is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Concept Questions on Go Linked List Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. What is the time to find the 5th node of a linked list?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is O(n), because you must start at the head and follow <code>Next<\/code> four times. A slice gives you <code>numbers[4]<\/code> in O(1).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. What is the difference between a singly and a doubly linked list?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A singly linked node has only <code>Next<\/code>. A doubly linked node has <code>Prev<\/code> and <code>Next<\/code>, so you can walk both ways. You can also delete a node in O(1), at the cost of one more pointer per node.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. Why does Floyd&#8217;s cycle check always work?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inside a loop, the fast pointer moves one node closer to the slow pointer on every step. So the gap shrinks by one each time and must reach zero, which means they meet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\nfunc main() {\n    var head *Node \/\/ an empty list is just a nil pointer\n    fmt.Println(head == nil)\n    head = &amp;Node{Value: 5}\n    fmt.Println(head.Next == nil, head.Value)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true\ntrue 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An empty list is a nil head. A single node has a nil <code>Next<\/code>, which marks the end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What happens if you read Value from a nil node?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\nfunc main() {\n    head := &amp;Node{Value: 1}\n    fmt.Println(head.Next.Value) \/\/ head.Next is nil, so this panics\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>panic: runtime error: invalid memory address or nil pointer dereference<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the most common bug in Go linked list programs. Always check for <code>nil<\/code> before you follow a pointer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Coding Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. How do you count the nodes in a list?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ length walks the list once and counts each node.\nfunc length(head *Node) int {\n    count := 0\n    for current := head; current != nil; current = current.Next {\n        count++\n    }\n    return count\n}\n\nfunc main() {\n    head := &amp;Node{1, &amp;Node{2, &amp;Node{3, nil}}}\n    fmt.Println(length(head), length(nil))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop adds one for each node and stops at <code>nil<\/code>. So an empty list returns 0 without any special code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. How do you reverse a list with recursion?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ reverseRecursive reverses the rest, then hooks the current node on the end.\nfunc reverseRecursive(head *Node) *Node {\n    if head == nil || head.Next == nil {\n        return head \/\/ base case: 0 or 1 node is already reversed\n    }\n    newHead := reverseRecursive(head.Next) \/\/ Step 1: reverse everything after head\n    head.Next.Next = head                  \/\/ Step 2: the next node now points back to head\n    head.Next = nil                        \/\/ Step 3: head becomes the new last node\n    return newHead\n}\n\nfunc main() {\n    head := &amp;Node{1, &amp;Node{2, &amp;Node{3, nil}}}\n    for current := reverseRecursive(head); current != nil; current = current.Next {\n        fmt.Print(current.Value, \" \")\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3 2 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The function calls itself on a shorter list each time, until only one node is left. On the way back, each node is hooked behind the node that came after it. This is short, but it uses O(n) stack memory. For very long lists, the loop version from Program 6 is safer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. How do you remove duplicates from a sorted list?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one item in the list.\ntype Node struct {\n    Value int\n    Next  *Node\n}\n\n\/\/ removeDuplicates skips any next node with the same value as the current one.\nfunc removeDuplicates(head *Node) *Node {\n    current := head\n    for current != nil &amp;&amp; current.Next != nil {\n        if current.Value == current.Next.Value {\n            current.Next = current.Next.Next \/\/ drop the duplicate, stay on current\n        } else {\n            current = current.Next\n        }\n    }\n    return head\n}\n\nfunc main() {\n    head := &amp;Node{1, &amp;Node{1, &amp;Node{2, &amp;Node{3, &amp;Node{3, nil}}}}}\n    for current := removeDuplicates(head); current != nil; current = current.Next {\n        fmt.Print(current.Value, \" \")\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Because the list is sorted, duplicates sit next to each other. When the next node matches, you skip it and stay in place. That is why one pass is enough.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Design Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. When would you choose a linked list over a slice in Go?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Choose a list when you often insert or remove items in the middle and already hold a pointer to the spot. An LRU cache is the classic case. For almost everything else, a slice is faster.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. Why does an LRU cache use a doubly linked list and a map?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The map finds a node in O(1), and the doubly linked list moves that node to the front or removes the oldest one in O(1). Together, every operation is O(1). We build one in a later post in this series.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise Go linked list programs, write a function that adds two numbers stored as lists in reverse order. For example, 342 is <code>2 -&gt; 4 -&gt; 3<\/code> and 465 is <code>5 -&gt; 6 -&gt; 4<\/code>, and the answer 807 is <code>7 -&gt; 0 -&gt; 8<\/code>. Hint: walk both lists together and keep a carry value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/container\/list\">container\/list package documentation<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/tour\/moretypes\/1\">Pointers in the Tour of Go<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#Struct_types\">Struct types in the Go specification<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go linked list programs chain nodes together with a <code>Next<\/code> pointer and end at <code>nil<\/code>.<\/li>\n\n\n<li>Reverse a list with three pointers: prev, cur and next.<\/li>\n\n\n<li>Slow and fast pointers find the middle and detect cycles.<\/li>\n\n\n<li>A dummy node removes special cases for the head.<\/li>\n\n\n<li><code>container\/list<\/code> gives a ready doubly linked list, and generics make a type-safe one.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we build two data structures that sit on top of lists and slices. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-stack-and-queue-programs\/\">Go stack and queue programs<\/a>, with valid parentheses, a min stack and a circular queue. For more pointer practice, revisit the <a href=\"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/\">Go pointer programs<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go linked list programs are one of the most asked topics in coding interviews, because they test pointers, loops and careful thinking all at&hellip;<\/p>\n","protected":false},"author":1,"featured_media":839,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,95,11,119,104],"swapz_series":[],"class_list":["post-842","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-lang","tag-go-linked-list","tag-go-programs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Linked List Programs: Reverse, Cycle, Merge<\/title>\n<meta name=\"description\" content=\"Go linked list programs with output: insert, delete, reverse, middle node, cycle check, merge sorted lists and a generic list, plus interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Linked List Programs: Reverse, Cycle, Merge\" \/>\n<meta property=\"og:description\" content=\"Go linked list programs with output: insert, delete, reverse, middle node, cycle check, merge sorted lists and a generic list, plus interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-10T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"24 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Linked List Programs: Reverse, Middle, Cycle and Merge\",\"datePublished\":\"2026-10-10T03:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/\"},\"wordCount\":2017,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-linked-list-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Linked List\",\"Go Programs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/\",\"name\":\"Go Linked List Programs: Reverse, Cycle, Merge\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-linked-list-programs-featured.png\",\"datePublished\":\"2026-10-10T03:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:26+00:00\",\"description\":\"Go linked list programs with output: insert, delete, reverse, middle node, cycle check, merge sorted lists and a generic list, plus interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-linked-list-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-linked-list-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Linked List Programs featured image: reverse, middle and cycle output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-linked-list-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Linked List Programs: Reverse, Middle, Cycle and Merge\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Linked List Programs: Reverse, Cycle, Merge","description":"Go linked list programs with output: insert, delete, reverse, middle node, cycle check, merge sorted lists and a generic list, plus interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Linked List Programs: Reverse, Cycle, Merge","og_description":"Go linked list programs with output: insert, delete, reverse, middle node, cycle check, merge sorted lists and a generic list, plus interview Q&A.","og_url":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-10T03:30:00+00:00","article_modified_time":"2026-11-28T19:59:26+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"24 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Linked List Programs: Reverse, Middle, Cycle and Merge","datePublished":"2026-10-10T03:30:00+00:00","dateModified":"2026-11-28T19:59:26+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/"},"wordCount":2017,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Linked List","Go Programs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/","name":"Go Linked List Programs: Reverse, Cycle, Merge","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-featured.png","datePublished":"2026-10-10T03:30:00+00:00","dateModified":"2026-11-28T19:59:26+00:00","description":"Go linked list programs with output: insert, delete, reverse, middle node, cycle check, merge sorted lists and a generic list, plus interview Q&A.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-linked-list-programs-featured.png","width":2400,"height":1260,"caption":"Go Linked List Programs featured image: reverse, middle and cycle output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-linked-list-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Linked List Programs: Reverse, Middle, Cycle and Merge"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=842"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/842\/revisions"}],"predecessor-version":[{"id":899,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/842\/revisions\/899"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/839"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=842"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}