-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathListNode.java
More file actions
143 lines (103 loc) · 2.87 KB
/
Copy pathListNode.java
File metadata and controls
143 lines (103 loc) · 2.87 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package util;
/**
* Created by nikoo28 on 12/17/17 10:41 PM
*/
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
}
public ListNode insertAtBeginning(ListNode head, int valToInsert) {
// Create a node to insert
ListNode newNode = new ListNode(valToInsert);
// Assign the next of this node to head
newNode.next = head;
// Point the head of the list to this new node
head = newNode;
return head;
}
public ListNode insertAtEnd(ListNode head, int valToInsert) {
// Create a node to insert
ListNode newNode = new ListNode(valToInsert);
// Goto the end of the list
ListNode ptr = head;
while (ptr.next != null)
ptr = ptr.next;
// Assign the newNode at the end of the list
ptr.next = newNode;
// Return original head
return head;
}
public ListNode insertInMiddle(ListNode head, int valToInsert, int position) {
// Create a node to insert
ListNode newNode = new ListNode(valToInsert);
// Move to the position
ListNode ptr = head;
for (int i = 0; i < position; i++) {
ptr = ptr.next;
}
// Insert the node
newNode.next = ptr.next;
ptr.next = newNode;
// Return original head
return head;
}
public ListNode deleteFromBeginning(ListNode head) {
// Just move the head to the next position
head = head.next;
// Return the new head
return head;
}
public ListNode deleteAtEnd(ListNode head) {
// Move to the second last node
ListNode ptr = head;
while (ptr.next.next != null)
ptr = ptr.next;
// Point the next of second last node to null
ptr.next = null;
// Return original head
return head;
}
public ListNode deleteFromMiddle(ListNode head, int position) {
// Move to previous position of node to delete
ListNode ptr = head;
for (int i = 0; i < position - 1; i++) {
ptr = ptr.next;
}
// Get the node to delete
ListNode nodeToDelete = ptr.next;
// Get the address of node next to the node to be deleted
ListNode nextNode = nodeToDelete.next;
// Point the next of ptr to nextNode
ptr.next = nextNode;
// Return the original head
return head;
}
public void traverseALinkedList(ListNode head) {
ListNode temp = head;
// Run a loop until you reach null
while (temp != null) {
// Print the first value
System.out.println(temp.val);
// Move to the next node
temp = temp.next;
}
}
public static void main(String[] args) {
// Create 3 nodes
ListNode l1 = new ListNode(4);
ListNode l2 = new ListNode(8);
ListNode l3 = new ListNode(15);
// Link the nodes
l1.next = l2;
l2.next = l3;
l3.next = null;
// Print values
ListNode ptr = l1;
while(ptr != null) {
System.out.println(ptr.val);
ptr = ptr.next;
}
}
}