Skip to content

Commit 74d42da

Browse files
committed
Rotate a Linked LIst
1 parent c570ccd commit 74d42da

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
Problem Link: https://practice.geeksforgeeks.org/problems/rotate-a-linked-list/1
3+
4+
Given a singly linked list of size N. The task is to rotate the linked list counter-clockwise by k nodes,
5+
where k is a given positive integer smaller than or equal to length of the linked list.
6+
7+
Input Format:
8+
First line of input contains number of testcases T. For each testcase, first line of input contains length of
9+
linked list and next line contains linked list elements and last line contains k, by which linked list is to be rotated.
10+
11+
Output Format:
12+
For each testcase, print the rotated linked list.
13+
14+
User Task:
15+
The task is to complete the function rotate() which takes head reference as the first argument and k as the second argument.
16+
The printing is done automatically by the driver code.
17+
18+
Constratints:
19+
1 <= T <= 100
20+
1 <= N <= 103
21+
1 <= k <= 103
22+
23+
Example:
24+
Input:
25+
3
26+
8
27+
1 2 3 4 5 6 7 8
28+
4
29+
5
30+
2 4 7 8 9
31+
3
32+
2
33+
1 2
34+
4
35+
36+
Output:
37+
5 6 7 8 1 2 3 4
38+
8 9 2 4 7
39+
1 2
40+
41+
Explanation:
42+
Testcase 1: After rotating the linked list by 4 elements (anti-clockwise), we reached to node 5, which is (k+1)th node
43+
from beginning, now becomes head and tail of the linked list is joined to the previous head.
44+
"""
45+
class Node:
46+
def __init__(self, data):
47+
self.data = data
48+
self.next = None
49+
class LinkedList:
50+
def __init__(self):
51+
self.head = None
52+
def push(self, new_data):
53+
new_node = Node(new_data)
54+
new_node.next = self.head
55+
self.head = new_node
56+
def printList(self):
57+
temp = self.head
58+
while(temp):
59+
print(temp.data, end=" ")
60+
# arr.append(str(temp.data))
61+
temp = temp.next
62+
print("")
63+
if __name__ == '__main__':
64+
start = LinkedList()
65+
t = int(input())
66+
while(t > 0):
67+
llist = LinkedList()
68+
n = int(input())
69+
values = list(map(int, input().strip().split()))
70+
for i in reversed(values):
71+
llist.push(i)
72+
k = int(input())
73+
new_head = rotateList(llist.head, k)
74+
llist.head = new_head
75+
llist.printList()
76+
t -= 1
77+
78+
''' This is a function problem.You only need to complete the function given below '''
79+
# Your task is to complete this function
80+
'''
81+
class Node:
82+
def __init__(self, data):
83+
self.data = data
84+
self.next = None
85+
'''
86+
# This function should rotate list counter-
87+
# clockwise by k and return new head (if changed)
88+
def rotateList(head, k):
89+
# code here
90+
if not head:
91+
return None
92+
l = length(head)
93+
k = k%l
94+
if not k:
95+
return head
96+
cur = head
97+
for _ in range(k-1):
98+
cur = cur.next
99+
newHead = cur.next
100+
newCur = cur.next
101+
cur.next = None
102+
while newCur.next:
103+
newCur = newCur.next
104+
newCur.next = head
105+
return newHead
106+
107+
def length(head):
108+
l = 0
109+
while head:
110+
l += 1
111+
head = head.next
112+
return l

0 commit comments

Comments
 (0)