Skip to main content

Command Palette

Search for a command to run...

Middle of the Linked List

Published
1 min read
Middle of the Linked List
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given the head of a singly linked list, return the middle node of the linked list.

If there are two middle nodes, return the second middle node.

LeetCode Problem - 876

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */

class Solution {
    public ListNode middleNode(ListNode head) {
        // Initialize the length counter and pointer to traverse the list
        int length = 0;
        ListNode point = head;

        // Traverse the entire linked list to calculate its length
        while (point != null) {
            length++;
            point = point.next;
        }

        // Calculate the index of the middle node
        int idx = length / 2;

        // Reset the pointer to the head of the list
        point = head;

        // Traverse the list again to reach the middle node
        while (idx > 0) {
            idx--;
            point = point.next;
        }

        // Return the middle node
        return point;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.