Delete First Node from a Linked List in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 06:04:41

619 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.Example Live Demopackage 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 = ... Read More

Add Node at the End of a Given Linked List in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 06:00:37

559 Views

ExampleNext5NullApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, create a new node and return that node.Step 3 − If head is not nil, traverse till the second last of the linked list.Example Live Demopackage 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 {     ... Read More

Add First Node in a Given Linked List using Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:54:48

332 Views

ExampleApproach to solve this problemStep 1 − Define a method that accepts the head of the linked list.Step 2 − If head == nil, create a new node and return that node.Step 3 − If head is not nil, then update the head of the input linked list.Example Live Demopackage 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 {     ... Read More

Reverse a Given Linked List in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:50:09

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.Example Live Demopackage 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

Count the Number of Nodes in a Linked List Using Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:48:36

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.Example Live Demopackage 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: ")   ... Read More

Define a Singly Linked List in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:45:22

587 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.Example Live Demopackage 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    } } ... Read More

Convert Integer to Binary Representation in Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:43:23

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.Example Live Demopackage 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.", ... Read More

Traverse a Given Binary Tree in Preorder Traversal (Recursive) using Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:40:47

666 Views

ExampleSuppose we have the following binary tree.Preorder Tree Traversal Output: 1, 2, 4, 5, 3, 6, 7.Approach to solve this problemStep 1 − If the root node of the given tree is nil, then return; else, follow the steps given below.Step 2 − Print the root node data.Step 3 − Traverse the Left sub-tree.Step 4 − Traverse the Right sub-tree.Example Live Demopackage main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)PreOrderTraversal(){    if root !=nil{       fmt.Printf("%d ", root.data)       root.left.PreOrderTraversal()       root.right.PreOrderTraversal()   ... Read More

Find Odd Occurring Elements in an Array using Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:37:15

314 Views

ExamplesFor example, arr = [1, 4, 5, 1, 4, 5, 1] => Odd-occurring element in the array is: 1Approach to solve this problemStep 1 − Define method that accepts an array.Step 2 − Declare a xor variable, i.e., xor := 0.Step 3 − Iterate input array and perform xor operation with each element of the array.Step 4 − At the end, return xor.Example Live Demopackage main import (    "fmt" ) func FindOddOccurringElement(arr []int) int{    xor := 0    for i := 0; i < len(arr); i++ {       xor = xor ^ arr[i]    }    return ... Read More

Check Power of 4 for a Given Number in Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:35:10

207 Views

ExamplesFor example, n = 12 => 12 is not the power of 4.For example, n = 64 => 64 is the power of 4.Approach to solve this problemStep 1 − Define a method that accepts a number n.Step 2 − Divide log(n) by log(4), store in res.Step 3 − If the floor of res is same as the res, then print that n is the power of 4.Step 4 − Else, print that n is not the power of 4.Example Live Demopackage main import (    "fmt"    "math" ) func CheckPowerOf4(n int){    res := math.Log(float64(n)) / math.Log(float64(4))    if ... Read More

Advertisements