DL List delete last node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func DeleteLast(head **DLNode) { | |
if *head == nil { | |
fmt.Println("\nList is empty") | |
return | |
} | |
// If there is only 1 node in the list | |
if (*head).Next == nil { | |
*head = nil | |
return | |
} | |
current := *head | |
// Find the last node | |
for current.Next != nil { | |
current = current.Next | |
} | |
fmt.Println("\nDeleting last node: ", current) | |
// Change the Next pointer of the penultimate node to nil | |
current = current.Prev | |
current.Next = nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment