File tree 4 files changed +59
-0
lines changed
0237_delete_node_in_a_linked_list
4 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ continually updating 😃.
69
69
* [ 92. Reverse Linked List II] ( src/0092_reverse_linked_list_2/reverse_linked_list2.go )
70
70
* [ 203. Remove Linked List Elements] ( src/0203_remove_linked_list_elements/remove_linked_list_elements.go )
71
71
* [ 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 )
72
73
73
74
### Dynamic Programming
74
75
* [ 62. Unique Paths] ( ./src/0062_unique_paths/unique_paths.go )   ;  ;  ; * ` array ` *
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 72
72
| 0219| [ 219. Contains Duplicate II] ( 0219_contains_duplicate_2/contains_duplicate_2.go ) | Easy| * ` map ` * |
73
73
| 0226| [ Invert Binary Tree] ( ./0226_invert_binary_tree/invert_binary_tree.go ) | Easy| * ` recursion; ` * * ` binary tree ` * |
74
74
| 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 ` * |
75
76
| 0283| [ Move Zeroes(solution1)] ( ./0283_move_zeroes/move_zeroes.go ) <br /> [ Move Zeroes(solution2)] ( ./0283_move_zeroes/move_zeroes2.go ) | Easy| * ` array ` * |
76
77
| 0300| [ Longest Increasing Subsequence] ( ./0300_longest_increasing_subsequence/lis.go ) | Medium| * ` dp ` * |
77
78
| 0303| [ 303. Range Sum Query - Immutable] ( 0303_range_sum_query/rsqim.go ) | Easy||
You can’t perform that action at this time.
0 commit comments