Split Linked List Alternatingly | GFG POTD | 17-10-24 | Easy | GFG Problem of the day | C++ | Java

Поделиться
HTML-код
  • Опубликовано: 27 ноя 2024

Комментарии •

  • @ParasKaushik-e6i
    @ParasKaushik-e6i Месяц назад +1

    ❤❤

  • @codewithuday
    @codewithuday  Месяц назад

    class Solution {
    public:
    void insertAtTail(Node*&head , Node*&tail,Node* node){
    if(!head){
    head = node;
    tail = node;
    return ;
    }
    tail->next = node;
    tail = node;
    }
    // Function to split a linked list into two lists alternately
    vector alternatingSplitList(struct Node* head) {
    // Your code here
    if(!head || !head->next) return {head,nullptr};
    Node* node1 = nullptr , *node2 = nullptr ;
    Node* node1Tail = node1 , * node2Tail = node2;
    int i = 0;
    while(head){
    if(i&1) insertAtTail(node2,node2Tail,head);
    else insertAtTail(node1,node1Tail,head);
    head = head->next;
    i++;
    }
    node1Tail->next = nullptr ; node2Tail ->next = nullptr;
    return {node1 , node2};

    }
    };