-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelete Node in a Linked List.java
42 lines (33 loc) · 1.27 KB
/
Delete Node in a Linked List.java
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
// https://leetcode.com/problems/delete-node-in-a-linked-list/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
/*
OBJECTIVE: Remove node from linked list
Time Complexity: O(n) where n = # of remaining nodes inside the linked list from the given node's
position. The linked list is iterated through from node's position.
Space Complexity: O(1) because no additional space was needed
*/
// Create a pointer
ListNode curNode = node;
// Iterate list and make sure 2 nodes ahead of you isn't null
while (curNode.next.next != null) {
// Move neighboring node's value to current node
int nextValue = curNode.next.val;
curNode.val = nextValue;
curNode = curNode.next;
}
// At the second to last node, copy next node's value and set next as null
// NOTE: At this point, node is behind the last node of the linked list
int nextValue = curNode.next.val;
curNode.val = nextValue;
curNode.next = null;
}
}