forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_node_test.go
58 lines (45 loc) · 1.27 KB
/
delete_node_test.go
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
/*
Problem:
- Delete a node from a singly-linked list, given only a pointer to that node.
Approach:
- Since we don't have access to the previous node, simply copy the value and
pointer of the next node and copy them into the current node.
Solution:
- Cache the next node.
- If the next node is nil, it's the last node. Just simply return.
- Copy the current node's value to the next node's value
- Copy the node's pointer to the next node's pointer.
Cost:
- O(1) time and O(1) space.
*/
package interviewcake
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestDeleteNode(t *testing.T) {
// define test input.
t1 := common.NewListNode(1)
for i := 2; i <= 6; i++ {
t1.AddNext(i)
}
// deletes node 4.
deleteNode(t1.Next.Next.Next)
common.Equal(t, []int{1, 2, 3, 5, 6}, common.LinkedListToSlice(t1))
// deletes node 3.
deleteNode(t1.Next.Next)
common.Equal(t, []int{1, 2, 5, 6}, common.LinkedListToSlice(t1))
// deletes node 5.
deleteNode(t1.Next.Next)
common.Equal(t, []int{1, 2, 6}, common.LinkedListToSlice(t1))
}
func deleteNode(node *common.ListNode) {
nextNode := node.Next
// if the next node is nil, it's the last node. this implementation does
// not work.
if nextNode == nil {
return
}
node.Value = nextNode.Value
node.Next = nextNode.Next
}