
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Middle Element of Linked List in a Single Iteration using Golang
In golang data structures , a linked list has pointers connecting the items, and from the first node (head) to the last node, each node can be visited (tail) using next pointer. We will obtain the middle element of a linked list using two methods. The first approach depicts use of two-pointer approach and the second approach uses a counter variable to execute the program.
Method 1: Using two-pointer approach
In this method, the function navigates the linked list using two pointers, low and high. The high pointer takes two steps at a time, whereas the low pointer takes one step. The low pointer will be pointing to the middle member of the linked list when the high pointer reaches the end of the list.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Create a node struct with values of type int and next of type node.
Step 3 ? Create a function middle_node and in that function further create two pointers, low and high, and set them both to point to the linked list's head.
Step 4 ? Until high reaches the end of the list, iterate through the linked list.
Step 5 ? Move the low pointer one step and the high pointer two steps in each cycle.
Step 6 ? The low pointer will be pointing to the middle member of the linked list when the high pointer reaches the end of the list.
Step 7 ? In the next step, return the low pointer back to the function.
Step 8 ? This approach works because the middle element will be pointed to by the low pointer when the high pointer reaches the end of the list for a linked list with an odd number of entries. The low pointer will be pointing to the middle element of the two middle elements for a linked list with an even number of entries.
Example
In this example we used two pointer approach to get the middle element from the linked list.
package main import "fmt" type Node struct { value int next *Node } func middle_node(head *Node) *Node { low, high := head, head for high != nil && high.next != nil { low = low.next high = high.next.next } return low //the low is pointing to middle element } func main() { head := &Node{value: 10} //store values in the linked list head.next = &Node{value: 20} head.next.next = &Node{value: 30} head.next.next.next = &Node{value: 40} head.next.next.next.next = &Node{value: 50} node := middle_node(head) //obtain the middle node from the linked list fmt.Println("The middle node in the linked list is:", node.value) }
Output
The middle node in the linked list is: 30
Method 2: Using counter variable
In this approach, we start by counting how many elements there are in the linked list. The linked list is then iterated through a second time, stopping at the middle entry by iterating count/2 times. The output will be a middle node printed on the console using fmt package.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Create a node struct with values of type int and next of type node.
Step 3 ? Create a function middle_node and in that particular function initialize a counter variable to 0 and a pointer node pointing to the head of the linked list.
Step 4 ? Iterate through the linked list, incrementing the counter for each node and moving node to the next node until node reaches the end of the list.
Step 5 ? Initialize node to the head of the linked list again.
Step 6 ? Iterate count/2 times, moving node to the next node each time.
Step 7 ? Return node, which will be pointing to the middle element of the linked list.
Step 8 ? Print the node received in main function using fmt.Println() function where ln means new line.
Example
In this example we will use counter variable to find the middle element from the linked list.
package main import "fmt" type Node struct { value int next *Node } func middle_node(head *Node) *Node { count := 0 node := head for node != nil { count++ node = node.next } node = head for i := 0; i < count/2; i++ { node = node.next } return node //here node points to middle element } func main() { head := &Node{value: 10} //fill the linked list with required values head.next = &Node{value: 20} head.next.next = &Node{value: 30} head.next.next.next = &Node{value: 40} head.next.next.next.next = &Node{value: 50} node := middle_node(head) //obtain the middle element in the node fmt.Println("The middle node in the linked list is:", node.value) }
Output
The middle node in the linked list is: 30
Conclusion
We executed the program of getting the middle element of a linked list in single iteration using two methods. In the first method we used two pointer approach and in the second example we used counter variable to keep track of elements of linked list.