Computer Science, asked by naimeshanaime2001, 9 days ago

Question: 4 Given a linked list, return the linked list after rearranging the nodes in a spiral fashion i.e. start from the center and move towards the end, picking a node from the left side and then the right side from the center. Example: Input: 1 -> 2-> 3-> 4 -> 5 -> 6 -> 7 Output: 4 -> 3 -> 5->2-> 6 -> 1 -> 7 Explanation: 4 is at the center Input: 1 -> 2-> 3 -> 4 -> 5-> 6 Output: 3-> 4->2 -> 5->1 -> 6 Explanation: Center is in between 3 and 4 Expected time complexity: O(n) Expected space complexity: 0(1) n = number of nodes in the linked list​

Answers

Answered by kunalkundu625
0

Explanation:

#include<stdio.h>

int main(){

int arr[]={1,2,3,4,5,6};

int n=sizeof(arr)/sizeof(arr[0]);

int low=0;

int high=n-1;

int c,a=0;

int mid=(low+high)/2;

for(int i=1;i<=mid;i++){

if(i==1)

{

printf(" %d",arr[mid]);

if(n%2==0)

{

printf(" %d",arr[mid+1]);

c=a=1;

}

}

printf(" %d",arr[mid-i]);

printf(" %d",arr[mid+i+a]);

}

}

Similar questions