File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
src/me/ramswaroop/linkedlists Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ package me .ramswaroop .linkedlists ;
2
+
3
+ import me .ramswaroop .common .SingleLinkedList ;
4
+ import me .ramswaroop .common .SingleLinkedNode ;
5
+
6
+ /**
7
+ * Created by IntelliJ IDEA.
8
+ *
9
+ * @author: ramswaroop
10
+ * @date: 6/23/15
11
+ * @time: 7:52 PM
12
+ */
13
+ public class MoveLastNodeToFirst <E extends Comparable <E >> extends SingleLinkedList <E > {
14
+
15
+ public static <E extends Comparable <E >> void moveLastNodeToFirst (SingleLinkedList <E > list ) {
16
+ if (list .size <= 1 ) return ;
17
+
18
+ SingleLinkedNode <E > curr = list .getNode (0 ), prev = curr ;
19
+ while (curr .next != null ) {
20
+ prev = curr ;
21
+ curr = curr .next ;
22
+ }
23
+ prev .next = null ;
24
+ curr .next = list .head ;
25
+ list .head = curr ;
26
+ }
27
+
28
+ public static void main (String a []) {
29
+ SingleLinkedList <Integer > linkedList = new SingleLinkedList <>();
30
+ linkedList .add (00 );
31
+ linkedList .add (11 );
32
+ linkedList .add (22 );
33
+ linkedList .add (33 );
34
+ linkedList .printList ();
35
+ moveLastNodeToFirst (linkedList );
36
+ linkedList .printList ();
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments