-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
84 lines (55 loc) · 1.24 KB
/
Copy pathLinkedList.java
File metadata and controls
84 lines (55 loc) · 1.24 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
84
/*
Node is defined as
class Node {
int data;
Node next;
}
*/
/*These methods shown below are "method-only" submission.
You only need to complete these methods.
In order to test these methods, I would recommend that
you create your own main method and comment out the other
methods that are not in use!
*/
/*
Insert Node at the beginning of a linked list
head pointer input could be NULL as well for empty list
*/
Node Insert(Node head,int x) {
Node mynode = new Node();
mynode.data = x;
mynode.next = head;
return mynode;
}
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
*/
Node Insert(Node head,int data) {
if(head==null){
head = new Node();
head.data = data;
} else{
Node current = head;
while(current.next!=null){
current = current.next;
}
current.next = new Node();
current.next.data = data;
}
return head;
}
/*
Prints elements of a linked list
head pointer input could be NULL as well for empty list
*/
void Print(Node head) {
if(head==null){
return;
}
Node temp = head;
while(temp!=null){
System.out.println(temp.data);
temp=temp.next;
}
}