Middle Node of Singly Linked List

To find the middle node in Singly Linked List:

  1. Create two node pointers: temp and middle.
  2. Initialize temp and middle with head.
  3. Move middle pointer by one.
  4. Move temp pointer by two.
  5. Iterate 3 and 4, until temp becomes NULL.
int middleNode(){
  struct node *temp, *middle;
  temp = middle = head;
  while(temp!=NULL){
    if(temp->next==NULL){
        break;
    }
    middle = middle->next;
    temp = temp->next->next;
  }
  return middle->data;
}






Related Programs


  1. Create Singly Linked List with N number of nodes and display.
  2. Perform insertion operation on Singly Linked List.
  3. Perform deletion operation on Singly Linked List.
  4. Reverse of Singly Linked List.
  5. Move last node of Singly Linked List to first.
  6. Swap first and last nodes in a Singly Linked List.
  7. Find the length of Singly Linked List.
  8. Find middle node of Singly Linked List

No comments:

Post a Comment

Total Pageviews