Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26c3aeb

Browse files
LC#237 delete the given node in linked list, no head given
1 parent 33ee000 commit 26c3aeb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package LinkedList;
2+
3+
/*
4+
* Write a function to delete a node in a singly-linked list. You will not be given access to the
5+
* head of the list, instead you will be given access to the node to be deleted directly.
6+
* It is guaranteed that the node to be deleted is not a tail node in the list.
7+
*/
8+
public class DeleteNode237 {
9+
10+
public class ListNode {
11+
int val;
12+
ListNode next;
13+
14+
ListNode(int x) {
15+
val = x;
16+
}
17+
}
18+
19+
public void deleteNode(ListNode node) {
20+
21+
ListNode currNext = node.next;
22+
23+
int tempValue = node.val;
24+
node.val = currNext.val;
25+
currNext.val = tempValue;
26+
27+
node.next = currNext.next;
28+
}
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /