Skip to content

Commit 7f8d9d0

Browse files
authored
237 solved. (#57)
1 parent c3542e7 commit 7f8d9d0

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ continually updating 😃.
6969
* [92. Reverse Linked List II](src/0092_reverse_linked_list_2/reverse_linked_list2.go)
7070
* [203. Remove Linked List Elements](src/0203_remove_linked_list_elements/remove_linked_list_elements.go)
7171
* [206. Reverse Linked List](src/0206_reverse_linked_list/reverse_linked_list.go)
72+
* [237. Delete Node in a Linked List](src/0237_delete_node_in_a_linked_list/dniall.go)
7273

7374
### Dynamic Programming
7475
* [62. Unique Paths](./src/0062_unique_paths/unique_paths.go)   *`array`*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
237. Delete Node in a Linked List
3+
https://leetcode.com/problems/delete-node-in-a-linked-list/
4+
5+
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
6+
7+
Note:
8+
The linked list will have at least two elements.
9+
All of the nodes' values will be unique.
10+
The given node will not be the tail and it will always be a valid node of the linked list.
11+
Do not return anything from your function.
12+
*/
13+
// time: 2019-01-07
14+
15+
package dniall
16+
17+
// ListNode Definition for singly-linked list.
18+
type ListNode struct {
19+
Val int
20+
Next *ListNode
21+
}
22+
23+
// time complexity: O(1)
24+
// space complexity: O(1)
25+
func deleteNode(node *ListNode) {
26+
// The linked list will have at least two elements.
27+
// All of the nodes' values will be unique.
28+
// The given node will not be the tail and it will always be a valid node of the linked list.
29+
node.Val = node.Next.Val
30+
node.Next = node.Next.Next
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dniall
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func createSinglyLinkedList(nums []int) *ListNode {
9+
head := &ListNode{}
10+
cur := head
11+
12+
for _, num := range nums {
13+
cur.Next = &ListNode{Val: num}
14+
cur = cur.Next
15+
}
16+
return head.Next
17+
}
18+
19+
func TestDeleteNode(t *testing.T) {
20+
root := createSinglyLinkedList([]int{4, 5, 1, 9})
21+
node := root.Next
22+
expected := createSinglyLinkedList([]int{4, 1, 9})
23+
if deleteNode(node); !reflect.DeepEqual(expected, root) {
24+
t.Errorf("expected %v, got %v", expected, root)
25+
}
26+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
|0219|[219. Contains Duplicate II](0219_contains_duplicate_2/contains_duplicate_2.go)|Easy|*`map`*|
7373
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|
7474
|0235|[235. Lowest Common Ancestor of a Binary Search Tree](0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)|Easy|*`recursion; `* *`binary tree`*|
75+
|0237|[237. Delete Node in a Linked List](0237_delete_node_in_a_linked_list/dniall.go)|Easy|*`linked list`*|
7576
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
7677
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|
7778
|0303|[303. Range Sum Query - Immutable](0303_range_sum_query/rsqim.go)|Easy||

0 commit comments

Comments
 (0)