java: add «237. Delete Node in a Linked List»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
155b63cc56
commit
cf3bbb132f
1 changed files with 21 additions and 0 deletions
21
java/delete-node-in-a-linked-list.java
Normal file
21
java/delete-node-in-a-linked-list.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode(int
|
||||
* x) { val = x; } }
|
||||
*/
|
||||
class Solution {
|
||||
private boolean isLastNode(ListNode node) {
|
||||
return node != null && node.next == null;
|
||||
}
|
||||
|
||||
public void deleteNode(ListNode node) {
|
||||
while (node != null) {
|
||||
node.val = node.next.val;
|
||||
|
||||
if (isLastNode(node.next)) {
|
||||
node.next = null;
|
||||
}
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue