-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteNode.java
More file actions
35 lines (29 loc) · 820 Bytes
/
Copy pathDeleteNode.java
File metadata and controls
35 lines (29 loc) · 820 Bytes
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
* Write a function to delete a node (except the tail) in a singly linked list,
* given only access to that node.
*/
public class Solution {
public void deleteNode(ListNode node) {
int value = node.val;
ListNode next_node = node.next;
while(next_node != null){
if(next_node != null)
node.val = next_node.val;
if(next_node.next == null){
node.next = null;
next_node = null;
return;
}
else{
node = node.next;
next_node = node.next;
}
}
}
}