Go Programming Articles

Page 6 of 86

Golang Program to convert an integer into binary representation

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 4K+ Views

ExamplesFor example, n = 1 (Binary Representation of 1: 1)For example, n = 5 (Binary Representation of 5: 101)For example, n = 20 (Binary Representation of 5: 10100)For example, n = 31 (Binary Representation of 31: 11111)Approach to solve this problemStep 1 − Define a method that accepts an integer, n.Step 2 − Convert n into binary representation using golang packageStep 3 − Return the converted binary representation.Examplepackage main import (    "fmt"    "strconv" ) func IntegerToBinary(n int) string {    return strconv.FormatInt(int64(n), 2) } func main(){    n := 1    fmt.Printf("Binary Representation of %d is %s.", n, ...

Read More

Golang Program to define a singly linked list.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 642 Views

ExamplesApproach to solve this problemStep 1 − Let’s define a structure of the node.Step 2 − Make the Linked List such that the previous node would store the address of the next node.Examplepackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int) *Node{    var n Node    n.value = value    n.next = nil    return &n } func TraverseLinkedList(head *Node){    fmt.Printf("Linked List: ")    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next    } } func main(){    head := NewNode(30)    head.next = NewNode(10)    head.next.next = NewNode(40)    head.next.next.next = NewNode(40)    TraverseLinkedList(head) }OutputLinked List: 30 10 40 40

Read More

Golang Program to count the number of nodes in a linked list.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 5K+ Views

ExamplesApproach to solve this problemStep 1 − Define a method that accepts the head of the linked list.Step 2 − Initialize a variable, count := 0.Step 3 − Iterate the given linked list till it reaches the last node.Step 4 − Increase the count by 1 in the loop.Step 5 − Return count.Examplepackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func CountNodes(head *Node){    fmt.Printf("Input Linked List is: ")    count ...

Read More

Golang Program to reverse a given linked list.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 2K+ Views

ExamplesApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return; else, call ReverseLinkedList, recursively.Step 3 − Print head.value at the end.Examplepackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    fmt.Printf("Input Linked List is: ")    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       ...

Read More

Golang Program to delete the first node from a linked list.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 682 Views

ExampleApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − Go to the next node and return the updated head.Examplepackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next ...

Read More

Golang Program to delete the last node from a linked list.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 520 Views

ExamplesApproach to solve this problem −Step 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − Go to the next node and return the updated head.Examplepackage main import (    "fmt" ) type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)     ...

Read More

Golang Program to update the first node value in a linked list.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 282 Views

ExampleApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − Else, update the first node value to 29.Examplepackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next    } ...

Read More

Golang Program to update the last node value in a linked list.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 344 Views

ExampleApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − Else, update the last node value to 41.Examplepackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next    } ...

Read More

Golang Program to insert a new node after the Kth node.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 203 Views

ExamplesAdd node 15 after 10 value node.Approach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − Iterate the given linked list.Step 4 − If temp.value is 10, then add the node 15 as the next node.Step 5 − If node value 10 is not found, return the head without adding any node.Examplepackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value   ...

Read More

Golang program to delete the node after the Kth node.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 196 Views

ExamplesDelete node after 10 value node.Approach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − Iterate the given linked list.Step 4 − If temp.value is 10, then override that node’s next value with its next node’s next value.Step 5 − If node value 10 is not found, return the head without deleting any node.Examplepackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = ...

Read More
Showing 51–60 of 852 articles
« Prev 1 4 5 6 7 8 86 Next »
Advertisements