-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
42 lines (31 loc) · 880 Bytes
/
Copy pathLinkedList.java
File metadata and controls
42 lines (31 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class LinkedList {
Node<Integer> head;
public void push (int element){
Node new_node = new Node(element);
if (head == null){
head = new_node;
}
else {
Node last = head;
while (last.next != null){
last = last.next;
}
last.next = new_node;
}
}
public void print(){
Node node = head;
while (node != null){
System.out.print(node.data+" ");
node = node.next;
}
System.out.println();
}
public Node<Integer> reverse (Node<Integer> head){
if (head == null || head.next == null) return head;
Node<Integer> ans = reverse(head.next);
head.next.next = head;
head.next = null;
return ans;
}
}