Given the head of a linked list, remove the nth node from the end of the list and return its head.
Example 1:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Example 2:
Input: head = [1], n = 1
Output: []
Example 3:
Input: head = [1,2], n = 1
Output: [1]
Constraints:
Follow up: Could you do this in one pass?
func removeNthFromEnd(head *ListNode, n int) *ListNode {
count := 0
h := head
for h != nil {
h = h.Next
count++
}
index := count - n
h = head
prev := h
if index == 0 {
if h.Next == nil {
return nil
} else if h.Next.Next != nil {
h.Val = h.Next.Val
h.Next = h.Next.Next
return head
} else if h.Next != nil {
h.Val = h.Next.Val
h.Next = nil
return head
}
}
for index != 0 {
prev = h
h = h.Next
index--
}
prev.Next = h.Next
return head
}
func removeNthFromEnd(head *ListNode, n int) *ListNode {
p1, p2 := head, head
for n > 0 {
p2 = p2.Next
n--
}
if p2 == nil {
return p1.Next
}
for p2.Next != nil {
p1 = p1.Next
p2 = p2.Next
}
p1.Next = p1.Next.Next
return head
}
func removeNthFromEnd(head *ListNode, n int) *ListNode {
length := getLengthOfLinkedList(head)
// fmt.Printf("length: %d\n", length)
toRemove := length - n
// fmt.Printf("toRemove: %d\n", toRemove)
if toRemove == 0 {
return head.Next
}
tmp := head
tmpHead := tmp
for i := 0; i < length && tmp != nil; i++ {
if toRemove-1 == i {
// fmt.Printf("to remove: %d, ", tmp.Val)
remove := tmp.Next
tmp.Next = remove.Next
} else {
// fmt.Printf("next: %d, ", tmp.Val)
tmp = tmp.Next
}
}
return tmpHead
}
func getLengthOfLinkedList(h *ListNode) int {
tmp, count := h, 0
for (tmp != nil) {
count++
tmp = tmp.Next
}
return count
}
func removeNthFromEnd(head *ListNode, n int) *ListNode {
slow, fast, count := head, head.Next, 1
if fast != nil {
for fast != nil {
fast = fast.Next
count ++
}
}
fast = head.Next
diff := count - n
if diff == 0 {
return head.Next
}
for i:=1; i<diff;i++{
fast = fast.Next
slow = slow.Next
}
slow.Next = fast.Next
return head
}
func removeNthFromEnd(head *ListNode, n int) *ListNode {
start := &ListNode{}
start.Next = head
fast, slow := start, start
for i := 0; i < n; i++ {
fast = fast.Next
}
for fast.Next != nil {
fast = fast.Next
slow = slow.Next
}
slow.Next = slow.Next.Next
return start.Next
}
func removeNthFromEnd(head *ListNode, n int) *ListNode {
zeroNode := ListNode{
Val: 0,
Next: head,
}
slow := &zeroNode
fast := &zeroNode
for n > 0 && fast.Next != nil {
fast = fast.Next
n--
}
for fast.Next != nil {
slow = slow.Next
fast = fast.Next
}
slow.Next = slow.Next.Next
return zeroNode.Next
}
func removeNthFromEnd(head *ListNode, n int) *ListNode {
dummy := &ListNode{Next: head}
first := head
for i:=0; i<n; i++ {
first = first.Next
}
second := dummy
for first != nil {
second = second.Next
first = first.Next
}
second.Next = second.Next.Next
return dummy.Next
}
[Go] Linked List - Reverse Linked List (0) | 2022.05.02 |
---|---|
[Go] Linked List - Summary - Two-Pointer in Linked List from Java to Go (0) | 2022.05.01 |
[Go] Linked List - Linked List Cycle2(Detect Cycle) (0) | 2022.05.01 |
[Go] Linked List - Linked List Cycle (0) | 2022.04.30 |
[Go] Linked List - Singly Linked List - Design Linked List (0) | 2022.04.29 |
댓글 영역