File tree Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Original file line number Diff line number Diff line change 13
13
*/
14
14
public class SingleLinkedList <E > implements LinkedList <E > {
15
15
16
- SingleLinkedNode <E > head ;
17
- int size ;
16
+ public SingleLinkedNode <E > head ;
17
+ public int size ;
18
18
19
19
@ Override
20
20
public boolean add (E item ) {
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/19/15
11
+ * @time: 9:24 AM
12
+ */
13
+ public class ReverseList <E > extends SingleLinkedList <E > {
14
+
15
+ public void reverseList (SingleLinkedNode <E > node ) {
16
+ SingleLinkedNode <E > prev = node ;
17
+ SingleLinkedNode <E > curr = node .next ;
18
+ prev .next = null ; // this will be the last node after reversal, so make next of node = null
19
+ while (curr != null ) {
20
+ SingleLinkedNode <E > next = curr .next ;
21
+ curr .next = prev ;
22
+ prev = curr ;
23
+ curr = next ;
24
+ }
25
+ head = prev ;
26
+ }
27
+
28
+ public static void main (String a []) {
29
+ ReverseList <Integer > linkedList = new ReverseList <>();
30
+ linkedList .add (11 );
31
+ linkedList .add (22 );
32
+ linkedList .add (33 );
33
+ linkedList .add (44 );
34
+ linkedList .add (55 );
35
+ linkedList .printList ();
36
+ linkedList .reverseList (linkedList .getNode (0 ));
37
+ linkedList .printList ();
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments