How do you find the middle element in a linked list without using count?

Find the middle of a given linked list

Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3.
If there are even nodes, then there would be two middle nodes, we need to print the second middle element. For example, if given linked list is 1->2->3->4->5->6 then the output should be 4.

Finding middle element of Linked list using one loop

Write a function which will accept a pointer to the head of the linked list and returns a pointer to the middle node of the list.If the list given is

2 -> 3 -> 5 -> 1 -> 4 -> 8 -> 10

Then the function should return a pointer to Node with value 1. If the list has even number of elements

2 -> 3 -> 5 -> 1 -> 4 -> 8

Then you may return a pointer to either 5 or 1 [Since there is no clear middle node].

Another way to ask this question is “Given a linked list, write code to split is in two halves“. In this case also you have to write code to reach to the middle of the list and then update the pointers of the middle node .
Solution:
Simple way [Using 2 loops]

1. Traverse the list to count the total number of nodes in the list Node * temp = head; while[temp != NULL]{ temp = temp->link; count++; } 2. Traverse 'count/2' times forward and return address of the node. for[int i=0; inext; return head;

Using only one loop.

Refer to this post for the Idea. The idea is to take 2 pointer and move one pointer forward at twice the speed of 2nd pointer. When the first reach end of the list, second pointer will be at the middle.

Take 2 pointers, both pointing to the head of the list initially. While 1st pointer is not NULL Move 1st pointer to next of next Node. Move 2nd pointer to next Node. return 2nd pointer

Though this method is not more efficient than the previous one [both in terms of time & memory usage]. But sometimes interviewer asks that he want to use only one loop.

/** * Function to return the middle element in the list. * Returns NULL, if the list is empty. * Returns the 1st of the 2 middle elements if list has even number of node. **/ Node* getMiddle[Node *head] { if[head == NULL || head->next == NULL] return head; Node *ptr1 = head; // Will move 1 node frwd Node *ptr2 = head; // Will move 2 nodes frwd // we don't need to check for ptr1. because ptr2 ahead of ptr1 while[ptr2->next!= NULL && ptr2->next->next != NULL] { ptr2 = ptr2->next->next; ptr1 = ptr1->next; } return ptr1; }

Related Posts:

  • Find kth largest element in running stream of numbers
  • Max Distance between two occurrences of the same element
  • Swapping two variables without using third variable

4 Comments

  1. Ilango Victor says:
    August 2, 2016 at 4:21 pm

    semma

    Reply
  2. Mohit Makhija says:
    August 24, 2017 at 12:42 am

    here is my solution in c++, python and java
    //code2begin.blogspot.com/2017/03/finding-middle-node-in-linked-list.html

    Reply
  3. deepak says:
    April 12, 2020 at 4:25 pm

    Nice article well explained . there is good linkedlist examples collection

    Reply
  4. gols says:
    August 31, 2020 at 2:10 am

    thank you so much very helpful

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Save my name, email, and website in this browser for the next time I comment.

Current ye@r *
Leave this field empty

How do you find the middle of a linked list?

Solution Steps

  1. Create a pointer p , pointing to the head.
  2. Iterate over the linked list until p reaches to the end of the linked list, thereby find the length of the list.
  3. Set p to head again. Now, increment p length/2 times. Now, the p is at the middle of the linked list node. Return the value at p.

How do you find a node in a linked list?

Algorithm:

  1. Step 1: Declare the recursive function with parameters [Node * head, int data]
  2. Step 2: Put Node *temp = head, int index = 0;
  3. Step 3: Repeat Step 4 and 5 while [temp!= NULL]
  4. Step 4: if[temp -> data == data] return index.
  5. Step 5: else index++ and temp = temp->next;
  6. Step 6: return -1.

How do you find the middle element of a linked list in one pass in C++?

In order to find middle element of linked list in one pass, you need to maintain two pointers, one increment at each node while other increments after two nodes at a time. By having this arrangement, when first pointer reaches end, second pointer will point to middle element of linked list.

How do you add elements in the middle of a linked list in Java?

Algorithm

  1. Create a class Node which has two attributes: data and next. Next is a pointer to the next node in the list.
  2. Create another class InsertMid which has three attributes: head, tail, and size that keep tracks of a number of nodes present in the list.
  3. addNode[] will add a new node to the list: Create a new node.

1. Overview

In this tutorial, we're going to explain how to find the middle element of a linked list in Java.

We'll introduce the main problems in the next sections, and we'll show different approaches to solving them.

C

#include
#include
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
/* Function to get the middle of the linked list*/
void printMiddle[struct Node *head]
{
struct Node *slow_ptr = head;
struct Node *fast_ptr = head;
if [head!=NULL]
{
while [fast_ptr != NULL && fast_ptr->next != NULL]
{
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}
printf["The middle element is [%d] ", slow_ptr->data];
}
}
void push[struct Node** head_ref,int new_data]
{
/* allocate node */
struct Node* new_node =
[struct Node*]malloc[sizeof[struct Node]];
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = [*head_ref];
/* move the head to point to the new node */
[*head_ref] = new_node;
}
// A utility function to print a given linked list
void printList[struct Node *ptr]
{
while [ptr != NULL]
{
printf["%d->", ptr->data];
ptr = ptr->next;
}
printf["NULL "];
}
/* Drier program to test above function*/
int main[]
{
/* Start with the empty list */
struct Node* head = NULL;
int i;
for [i=5; i>0; i--]
{
push[&head, i];
printList[head];
printMiddle[head];
}
return 0;
}

Video liên quan

Bài mới nhất

Chủ Đề