-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListPractice.java
More file actions
49 lines (48 loc) · 1.03 KB
/
LinkedListPractice.java
File metadata and controls
49 lines (48 loc) · 1.03 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
public class LinkedListPractice {
}
class ListNode
{
int val;
ListNode next;
public ListNode(int num)
{
val = num;
}
/*Insert node after n0 */
public void insert(ListNode n0, ListNode node)
{
node.next = n0.next;
n0.next = node;
}
/*Delete the next node after n0 */
public void delete(ListNode n0)
{
if(n0.next==null)
return;
ListNode temp = n0.next.next;
n0.next.next = null;
n0.next = temp;
}
public ListNode access(ListNode head, int index)
{
for(int i=0;i<index;i++)
{
if(head == null)
return null;
head = head.next;
}
return head;
}
public int find(ListNode head, int val)
{
int index = 0;
while(head!=null)
{
if(head.val == val)
return index;
index++;
head = head.next;
}
return -1;
}
}