Given a singly linked list of size **N**. The task is to rotate the linked list counter-clockwise by **k** nodes, where k is a given positive integer smaller than or equal to length of the linked list.

**Input:**

First line of input contains number of testcases T. For each testcase, first line of input contains length of linked list and next line contains linked list elements and last line contains k, by which linked list is to be rotated.

**Output:**

For each testcase, print the rotated linked list.

**User Task:**

The task is to complete the function **rotate**() which takes head reference as the first argument and k as the second argument.

**Constratints:**

1 <= T <= 100

1 <= N <= 10^{3}

1 <= k <= 10^{3}

**Example:**

**Input:**

1

8

1 2 3 4 5 6 7 8

4

**Output:**

5 6 7 8 1 2 3 4

**Explanation:
Testcase 1: **After rotating the linked list by 4 elements (anti-clockwise), we reached to node 5, which is (k+1)th node from beginning, now becomes head and tail of the linked list is joined to the previous head.

Author: Karan Grover

lakshmi_pandey | 72 |

abbatta7 | 68 |

Ashish Kumar Vaishy | 62 |

aman19 | 58 |

Kumar Gaurav Singh | 58 |

saumitra13325 | 612 |

ashujack | 551 |

lakshmi_pandey | 544 |

aman19 | 538 |

piyushmittal25 | 514 |

blackshadows | 5249 |

akhayrutdinov | 5111 |

Ibrahim Nash | 5087 |

Quandray | 4354 |

sanjay05 | 3668 |

Login to report an issue on this page.