File tree Expand file tree Collapse file tree 1 file changed +82
-0
lines changed Expand file tree Collapse file tree 1 file changed +82
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ //reverse after k linked list
3
+ class classroomProblem {
4
+ Node head ;
5
+
6
+ class Node {
7
+ int data ;
8
+ Node next ;
9
+
10
+ Node (int d ) {
11
+ data = d ;
12
+ next = null ;
13
+ }
14
+ }
15
+
16
+ Node reverse (Node temp ) {
17
+
18
+ Node prev = null ;
19
+ Node next = null ;
20
+ Node curr =temp ;
21
+
22
+ while (curr != null )
23
+ {
24
+ next = curr .next ;
25
+ curr .next = prev ;
26
+ prev = curr ;
27
+ curr = next ;
28
+ }
29
+ return prev ;
30
+ }
31
+
32
+ public void reverseAfter (int k ) {
33
+
34
+ Node temp = head ;
35
+ if (head == null ) {
36
+ System .out .println ("no arrangement feasible" );
37
+ } else
38
+ {
39
+ int count = 0 ;
40
+ while (count != k - 1 )
41
+ {
42
+ temp = temp .next ;
43
+ count ++;
44
+ }
45
+ temp .next = reverse (temp .next );
46
+ }
47
+ }
48
+
49
+ public void push (int new_data ) {
50
+ Node temp = head ;
51
+ Node new_node = new Node (new_data );
52
+ if (head == null ) {
53
+ head = new_node ;
54
+ } else {
55
+ while (temp .next != null ) {
56
+ temp = temp .next ;
57
+ }
58
+ temp .next = new_node ;
59
+ }
60
+ }
61
+
62
+ public void printList () {
63
+ Node tnode = head ;
64
+ while (tnode != null ) {
65
+ System .out .print (tnode .data + " " );
66
+ tnode = tnode .next ;
67
+ }
68
+ }
69
+
70
+ public static void main (String [] args ) {
71
+ Scanner in = new Scanner (System .in );
72
+ classroomProblem obj = new classroomProblem ();
73
+ int no = in .nextInt ();
74
+ for (int i = 0 ; i < no ; i ++) {
75
+ int element = in .nextInt ();
76
+ obj .push (element );
77
+ }
78
+ int pos = in .nextInt ();
79
+ obj .reverseAfter (pos );
80
+ obj .printList ();
81
+ }
82
+ }
You can’t perform that action at this time.
0 commit comments