-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
83 lines (67 loc) · 1.93 KB
/
LinkedList.java
File metadata and controls
83 lines (67 loc) · 1.93 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class LinkedList{
public Node head;
public static int size;
public LinkedList(){
head=null;
size=0;
}
public static void addLast(int data, LinkedList list){
Node newNode = new Node(data);
newNode.nextNode=null;
if(list.head==null){
list.head=newNode;
}
else{
Node last=list.head;
while(last.nextNode!=null){
last=last.nextNode;
}
last.nextNode=newNode;
}
size++;
}
public static void addFront(int data,LinkedList list){
Node newNode=new Node(data,null);
if(list.head==null){
list.head=newNode;
}
else{
Node currentNode=list.head;
list.head=newNode;
list.head.nextNode=currentNode;
}
size++;
}
public static LinkedList reverseLinkedList(LinkedList list){
if(list.head==null || list.size==1)
return list;
else{
Node current=list.head;
Node previous=null;
Node forward=null;
while(current.nextNode!=null){
forward=current.nextNode;
current.nextNode=previous;
previous=current;
current=forward;
}
list.head=current;
list.head.nextNode=previous;
return list;
}
}
public static void printLinkedList(LinkedList list){
Node curNode=list.head;
if(curNode==null){
System.out.println("List is empty!");
}
else{
while(curNode!=null){
System.out.print(curNode.data+"->");
curNode=curNode.nextNode;
}
if(curNode==null)
System.out.print("null");
}
}
}