What if there are odd number of nodes?


  • 2
    S

    What if there are odd number of nodes?

    For example:

    L: L0

    What's the result?

    L: L0 -> L0?


  • 2
    S

    If length of list is 1, you do not have to change the list.

    If length of list is odd, like L: L0->L1->L2->L3->L4, you just need to follow the rules, change to L: L0->Ln->L1->Ln-1, i.e. L: L0->L4->L1->L3->L2.


  • 0
    X

    If your number of nodes is n, and you want to split the list into two halves in which the second half contains all the nodes that need to be inserted, then your second half should contain (n -1)/2 number of nodes.

    This formula covers both the old number and even number cases. In the example of 4 nodes, you only need to insert one node on the tail. In the example of 5 nodes, you need to move the last 2 nodes. And so on so forth.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.