Each node of a linked list is divided into which two parts

Given a singly linked list, the task is to split the given linked list into exactly three parts such that the maximum difference between the length of the split linked lists is minimum.

Examples:

Input: 1->2->3->4->5
Output:1->23->45

Explanation: 


Consider the splitting of the linked list as:
  1. 1->2: The size is 1.
  2. 3->4: The size is 1.
  3. 5: The size is 1.

The maximum difference between the length of any two splitted linked lists is 1, which is minimum.

Input: 7 -> 2 -> 1
Output:72

1

Approach: Follow the steps below to solve the given problem:

  • Initialize a vector, say ans[] that stores the split linked list
  • If the size of the given linked list is less than 3, then create size time linked list with only one node and 3 – size linked list with null nodes and add it to the ans vector and return.
  • Initialize a variable, say minSize as size / 3 that will be the minimum size of the linked list to be divided and rem as size % 3.
  • Iterate over the linked list until size becomes 0 and perform the following steps:
    • In each iteration, if rem equals 0, then iterate again minSize times into the linked list and add that linked list to the ans and decrement rem by 1.
    • Otherwise, iterate [minSize + 1] a number of times into the linked list and add that linked list to the ans.
  • After completing the above steps, print all the Linked List stored in the vector ans[].

Below is the implementation of the above approach:

#include

using namespace std;

class Node {

public:

    int data;

    Node* next;

};

int sizeOfLL[Node* head]

{

    int size = 0;

    while [head != NULL] {

        ++size;

        head = head->next;

    }

    return size;

}

vector Partition_of_list[Node* head]

{

    int size = sizeOfLL[head];

    Node* temp = head;

    vector ans;

    if [3 >= size] {

        while [temp != NULL] {

            Node* next = temp->next;

            temp->next = NULL;

            ans.push_back[temp];

            temp = next;

        }

        int y = 3 - size;

        while [y != 0] {

            ans.push_back[NULL];

            y--;

        }

    }

    else {

        int minSize = size / 3;

        int rem = size % 3;

        while [size > 0 && temp != NULL] {

            int m = 0;

            if [rem != 0] {

                m = minSize + 1;

                rem--;

            }

            else {

                m = minSize;

            }

            Node* curr = temp;

            for [int j = 1; j < m

                            && temp->next != NULL;

                 j++] {

                temp = temp->next;

            }

            if [temp->next != NULL] {

                Node* x = temp->next;

                temp->next = NULL;

                temp = x;

                ans.push_back[curr];

            }

            else {

                ans.push_back[curr];

                break;

            }

            size -= m;

        }

    }

    return ans;

}

void push[Node** head, int d]

{

    Node* temp = new Node[];

    temp->data = d;

    temp->next = NULL;

    if [[*head] == NULL]

        [*head] = temp;

    else {

        Node* curr = [*head];

        while [curr->next != NULL] {

            curr = curr->next;

        }

        curr->next = temp;

    }

}

void display[Node* head]

{

    while [head->next != NULL] {

        cout data next;

    }

    cout data = size] {

        while [temp != null] {

            Node next = temp.next;

            temp.next = null;

            ans.add[temp];

            temp = next;

        }

        int y = 3 - size;

        while [y != 0] {

            ans.add[null];

            y--;

        }

    }

    else {

        int minSize = size / 3;

        int rem = size % 3;

        while [size > 0 && temp != null] {

            int m = 0;

            if [rem != 0] {

                m = minSize + 1;

                rem--;

            }

            else {

                m = minSize;

            }

            Node curr = temp;

            for [int j = 1; j < m

                            && temp.next != null;

                 j++] {

                temp = temp.next;

            }

            if [temp.next != null] {

                Node x = temp.next;

                temp.next = null;

                temp = x;

                ans.add[curr];

            }

            else {

                ans.add[curr];

                break;

            }

            size -= m;

        }

    }

    return ans;

}

static Node push[Node head, int d]

{

    Node temp = new Node[];

    temp.data = d;

    temp.next = null;

    if [[head] == null]

        [head] = temp;

    else {

        Node curr = [head];

        while [curr.next != null] {

            curr = curr.next;

        }

        curr.next = temp;

    }

    return head;

}

static void display[Node head]

{

    while [head.next != null] {

        System.out.print[head.data+ "->"];

        head = head.next;

    }

    System.out.print[head.data+ "\n"];

}

public static void main[String[] args]

{

    Node head = null;

    head = push[head, 1];

    head = push[head, 2];

    head = push[head, 3];

    head = push[head, 4];

    head = push[head, 5];

    Vector v = Partition_of_list[head];

    for [int i = 0; i < v.size[]; i++] {

        display[v.get[i]];

    }

}

}

class Node:

    def __init__[self]:

        self.data = 0;

        self.next = next;

def sizeOfLL[head]:

    size = 0;

    while [head != None]:

        size += 1;

        head = head.next;

    return size;

def Partition_of_list[head]:

    size = sizeOfLL[head];

    temp = head;

    ans = [];

    if [3 >= size]:

        while [temp != None]:

            next = temp.next;

            temp.next = None;

            ans.append[temp];

            temp = next;

        y = 3 - size;

        while [y != 0]:

            ans.append[None];

            y-=1;

    else:

        minSize = size // 3;

        rem = size % 3;

        while [size > 0 and temp != None]:

            m = 0;

            if [rem != 0]:

                m = minSize + 1;

                rem-=1;

            else:

                m = minSize;

            curr = temp;

            for j in range[1,m]:

                temp = temp.next;

            if [temp.next != None]:

                x = temp.next;

                temp.next = None;

                temp = x;

                ans.append[curr];

            else:

                ans.append[curr];

                break;

            size -= m;

    return ans;

def push[head, d]:

    temp = Node[];

    temp.data = d;

    temp.next = None;

    if [[head] == None]:

        [head] = temp;

    else:

        curr = [head];

        while [curr.next != None]:

            curr = curr.next;

        curr.next = temp;

    return head;

def display[head]:

    while [head.next != None]:

        print[head.data , "->", end=""];

        head = head.next;

    print[head.data ];

if __name__ == '__main__':

    head = None;

    head = push[head, 1];

    head = push[head, 2];

    head = push[head, 3];

    head = push[head, 4];

    head = push[head, 5];

    v = Partition_of_list[head];

    for i in range[len[v]]:

        display[v[i]];

using System;

using System.Collections.Generic;

public class GFG {

public    class Node {

    public    int data;

    public    Node next;

    };

    static int sizeOfLL[Node head] {

        int size = 0;

        while [head != null] {

            ++size;

            head = head.next;

        }

        return size;

    }

    static List Partition_of_list[Node head] {

        int size = sizeOfLL[head];

        Node temp = head;

        List ans = new List[];

        if [3 >= size] {

            while [temp != null] {

                Node next = temp.next;

                temp.next = null;

                ans.Add[temp];

                temp = next;

            }

            int y = 3 - size;

            while [y != 0] {

                ans.Add[null];

                y--;

            }

        } else {

            int minSize = size / 3;

            int rem = size % 3;

            while [size > 0 && temp != null] {

                int m = 0;

                if [rem != 0] {

                    m = minSize + 1;

                    rem--;

                }

                else {

                    m = minSize;

                }

                Node curr = temp;

                for [int j = 1; j < m && temp.next != null; j++] {

                    temp = temp.next;

                }

                if [temp.next != null] {

                    Node x = temp.next;

                    temp.next = null;

                    temp = x;

                    ans.Add[curr];

                }

                else {

                    ans.Add[curr];

                    break;

                }

                size -= m;

            }

        }

        return ans;

    }

    static Node push[Node head, int d] {

        Node temp = new Node[];

        temp.data = d;

        temp.next = null;

        if [[head] == null]

            [head] = temp;

        else {

            Node curr = [head];

            while [curr.next != null] {

                curr = curr.next;

            }

            curr.next = temp;

        }

        return head;

    }

    static void display[Node head] {

        while [head.next != null] {

            Console.Write[head.data + "->"];

            head = head.next;

        }

        Console.Write[head.data + "\n"];

    }

    public static void Main[String[] args]

    {

        Node head = null;

        head = push[head, 1];

        head = push[head, 2];

        head = push[head, 3];

        head = push[head, 4];

        head = push[head, 5];

        List v = Partition_of_list[head];

        for [int i = 0; i < v.Count; i++] {

            display[v[i]];

        }

    }

}

     class Node {

        constructor[] {

            this.data = 0;

            this.next = null;

        }

    }

    function sizeOfLL[head] {

        var size = 0;

        while [head != null] {

            ++size;

            head = head.next;

        }

        return size;

    }

     function Partition_of_list[head] {

        var size = sizeOfLL[head];

        var temp = head;

        var ans = [];

        if [3 >= size] {

            while [temp != null] {

        var next = temp.next;

                temp.next = null;

                ans.push[temp];

                temp = next;

            }

            var y = 3 - size;

            while [y != 0] {

                ans.push[null];

                y--;

            }

        } else {

            var minSize = parseInt[size / 3];

            var rem = size % 3;

            while [size > 0 && temp != null] {

                var m = 0;

                if [rem != 0] {

                    m = minSize + 1;

                    rem--;

                }

                else {

                    m = minSize;

                }

                var curr = temp;

                for [var j = 1; j < m && temp.next != null; j++] {

                    temp = temp.next;

                }

                if [temp.next != null] {

                    var x = temp.next;

                    temp.next = null;

                    temp = x;

                    ans.push[curr];

                }

                else {

                    ans.push[curr];

                    break;

                }

                size -= m;

            }

        }

        return ans;

    }

    function push[head , d] {

        var temp = new Node[];

        temp.data = d;

        temp.next = null;

        if [[head] == null]

            [head] = temp;

        else {

            var curr = [head];

            while [curr.next != null] {

                curr = curr.next;

            }

            curr.next = temp;

        }

        return head;

    }

    function display[head] {

        while [head.next != null] {

            document.write[head.data + "->"];

            head = head.next;

        }

        document.write[head.data + "
"];

    }

        var head = null;

        head = push[head, 1];

        head = push[head, 2];

        head = push[head, 3];

        head = push[head, 4];

        head = push[head, 5];

        var v = Partition_of_list[head];

        for [i = 0; i < v.length; i++] {

            display[v[i]];

        }

Time Complexity: O[N]
Auxiliary Space: O[N]

Video liên quan

Chủ Đề