tharunbalaji31's blog

By tharunbalaji31, history, 23 months ago, In English

Intuition

The problem is to identify the middle element or node of the given Singly Linked List and return second middle element if the length of List is even. Basically there are two approaches to solve this problem:

  • Approach 1: Brute Force $$$TC:O(N) + O(N/2)$$$
  • Approach 2: Tortoise Rabbit Algorithm $$$TC:O(N/2)$$$

Approach 1

In the Brute Force approach, we will be calculating the length of Linked List by traversing through each element using temp pointer. Then calculate the half of length ($$$length/2$$$). Once again traverse through the Linked List and move the head variable until half of its length is reached, after reaching return the head pointer. Now the head pointer contains the chain from middle element of that List.

  • Time complexity: O(N) + O(N/2)
  • Space complexity: O(K)

Code

struct ListNode* middleNode(struct ListNode* head){
    struct ListNode* ptr = head;
    int length=0;
    int mid;

    while(ptr != NULL){
        ptr = ptr->next;
        length++;
    }

    mid = length/2;
    while(mid--){
            head = head->next;
    }

    return head;
}

Result

Runtime: 2ms Memory: 6MB

Approach 2

By using Tortoise Rabbit Algorithm, initially we assign two pointers tortoise and rabbit to head node. Now, we move both the pointers in such a way that Tortoise pointer moves one step forward asusual and Rabbit pointer moves two steps forward. So, when the Rabbit pointer reaches the end of List or the last node of List we have Tortoise pointer at the middle node of the List. Then we return the Tortoise pointer which contains the chain from middle element of the List.

  • Time complexity: O(N/2)
  • Space complexity: O(K)

Code

// In C
struct ListNode* middleNode(struct ListNode* head){
    struct ListNode *tortoise = head;
    struct ListNode *rabbit = head;

    while(rabbit != NULL && rabbit->next != NULL){
        tortoise = tortoise->next;
        rabbit = rabbit->next->next;
    }

    return tortoise;
}
// In Java
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode tortoise = head;
        ListNode rabbit = head;

        while(rabbit != null && rabbit.next != null){
            tortoise = tortoise.next;
            rabbit = rabbit.next.next;
        }

        return tortoise;
    }
}

Result

Runtime: 0ms Memory: 6MB

Upvote the solution, if you like the appraoch!

  • Vote: I like it
  • -2
  • Vote: I do not like it

| Write comment?
»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Wouldn't it fail when there are only two elements in the list?

NOTE: For four elements list, we take 2nd item as middle.

Example: 1-->2

Step 1. In starting rabbit will point to 1 and tortoise will also point to 1 (rabbit->1, tortoise->1)

Step 2. As rabbit is not NULL and rabbit->next is also not NULL, so now, tortoise->2 and rabbit->NULL

Step 3. Loop breaks and we return middle as tortoise(ie. 2)

As we can see from above steps, the middle is 2, when it should have been 1.