Found 26504 Articles for Server Side Programming

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

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 06:25:43

130 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.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 ... Read More

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

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

285 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.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 = temp.next   ... Read More

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

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 06:09:03

226 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.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 = temp.next   ... Read More

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

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 06:06:36

452 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.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)   ... Read More

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

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

Golang Program to add a node at the end of a given linked list.

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

Golang Program to add the first node in a given linked list.

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

Golang Program to reverse a given linked list.

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

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

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

Golang Program to define a singly linked list.

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

588 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

Advertisements