-
Notifications
You must be signed in to change notification settings - Fork 21
/
RemoveNthNodeFromEndOfList.go
48 lines (40 loc) 路 956 Bytes
/
RemoveNthNodeFromEndOfList.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
package RemoveNthNodeFromEndOfList
//Given a linked list, remove the nth node from the end of list and return its head.
//
//For example,
//
//Given linked list: 1->2->3->4->5, and n = 2.
//
//After removing the second node from the end, the linked list becomes 1->2->3->5.
//Note:
//Given n will always be valid.
//Try to do this in one pass.
//
//Accepted.
type ListNode struct {
Val int
Next *ListNode
}
func removeNthFromEnd(head *ListNode, n int) *ListNode {
stack := make([]*ListNode, 0)
var tmp *ListNode
node := head
for ; node != nil; node = node.Next {
stack = append(stack, node)
}
for ; n > 0 && len(stack) != 0; n-- {
tmp = stack[len(stack)-1]
stack = removeIndex(stack, len(stack)-1)
}
if tmp != nil {
if len(stack) != 0 {
stack[len(stack)-1].Next = tmp.Next
} else {
head = tmp.Next
}
}
return head
}
func removeIndex(s []*ListNode, index int) []*ListNode {
return append(s[:index], s[index+1:]...)
}