C Program for Folding a Linked List

C Program for Folding a Linked List

Learn how to fold a linked list

Today we learn how to write a code in C Program for fold a linked list, it is a way to divide the linked list and merge the two end of the linked list that make a fold . In below sections, we will learn steps and coe to perform the task.
For example :- Input : 10 12 3 14
Output : 10 14 12 3 (after folding linked list)

Steps required for folding a linked list :-

  • Firstly, Initialize a linked list.
  • Then find the middle point using two pointers at different speeds through the sequence of values until they both have equal values.
  •  Divide the linked list into two halves using these pointer named as slow and fast, then find middle.
  • Now reverse the second half of the linked list.
  • At last alternate merge of first and second halves.
  • Print the fold linked list.
Fold a Linked List in C
struct node
{
int data;
struct node *next;
};

Algorithm for a C Program for folding a linked list :-

  • MAKE A WHILE LOOP ( HEAD NODE 1 || HEAD NODE 2 )
  • IF ( HEAD NODE 1 )
  • THEN CURRENT →NEXT POINTER = HEAD NODE 1
  • CURRENT =CURRENT →NEXT POINTER HEAD NODE 1=HEAD NODE 1→ NEXT POINTER
  • END OF IF LOOP
  • ELSE ( HEAD NODE 2 )
  • THEN CURRENT→ NEXT POINTER = HEAD NODE 2
  • CURRENT = CURRENT→ NEXT POINTER
  • HEAD NODE 2 =HEAD NODE 2 →NEXT POINTER
  • END ELSE AND WHILE LOOP HEAD=HEAD → NEXT POINTER

C Program for folding a linked list :-

Run
#include< stdio.h>
#include< stdlib.h>

struct node {
    int data;
    struct node* next;
};

void reverse_list(struct node** head) {
    struct node* prev = NULL, *curr = *head, *next;
    while (curr) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }

    *head = prev;
}

void create(struct node** head, int data) {
    struct node* new_node;
    new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = *head;
    *head = new_node;
}

void print(struct node* head) {
    struct node* ptr = head;
    while (ptr) {
        printf("%d \n", ptr->data);
        ptr = ptr->next;
    }
    printf("\n");
}

struct node* newNode(int data) {
    struct node* new_node;
    new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

void fold(struct node** head) {
    struct node* slow = *head;
    struct node* fast = slow->next;
    struct node* curr;

    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }

    struct node* head1 = *head;
    struct node* head2 = slow->next;
    slow->next = NULL;

    reverse_list(&head2);

    *head = newNode(0);
    curr = *head;

    while (head1 || head2) {
        if (head1) {
            curr->next = head1;
            curr = curr->next;
            head1 = head1->next;
        }
        if (head2) {
            curr->next = head2;
            curr = curr->next;
            head2 = head2->next;
        }
    }
    *head = (*head)->next;
}

int main() {
    struct node* head = NULL;
    int n, i;
    int data[100];

    printf("Enter the size of the linked list: ");
    scanf("%d", &n);
    printf("Enter data for the linked list: ");
    
    for (i = 0; i < n; i++) {
        scanf("%d", &data[i]);
    }
    
    for (i = n - 1; i >= 0; i--) {
        create(&head, data[i]);
    }
    
    printf("Linked list data: ");
    print(head);
    fold(&head);
    printf("Linked list data after folding: ");
    print(head);
    
    return 0;
}

Output :-
Enter size of link list :-
5
Enter data for link list:-
12
4
51
20
68
Link list data:-
12
4
51
20
68
Link list data after fold :-
12
68
4
20
51

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java