Skip to content

Instantly share code, notes, and snippets.

@icaoberg
Last active January 15, 2025 09:57
Show Gist options
  • Save icaoberg/2912318 to your computer and use it in GitHub Desktop.
Save icaoberg/2912318 to your computer and use it in GitHub Desktop.
Simple linked list in Python
# Author: Ivan E. Cao-Berg ([email protected])
#
# Copyright (C) 2012-2025
# School of Computer Science
# Carnegie Mellon University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# For additional information visit http://www.andrew.cmu.edu/~icaoberg or
# send email to [email protected]
class LinkedList
def initialize
@list = []
end
# Appends a value to the end of the linked list
# @param value [Object] the value to be appended
# @return [Boolean] true if the value is successfully appended, false otherwise
def append(value = nil)
@list.push(value)
true
rescue
false
end
# Prepends a value to the start of the linked list
# @param value [Object] the value to be prepended
# @return [Boolean] true if the value is successfully prepended, false otherwise
def prepend(value = nil)
@list.unshift(value)
true
rescue
false
end
# Checks if the linked list contains a specific value
# @param value [Object] the value to check for
# @return [Boolean] true if the value exists, false otherwise
def contains?(value = nil)
@list.include?(value)
end
# Retrieves the value at a specific index
# @param index [Integer] the index to retrieve the value from
# @return [Object, nil] the value at the specified index or nil if index is invalid
def get(index)
@list.at(index)
rescue
nil
end
# Returns the size of the linked list
# @return [Integer] the number of elements in the list
def size
@list.size
end
# Removes the first element from the linked list
# @return [Boolean] true if an element is successfully removed, false otherwise
def remove
[email protected]?
rescue
false
end
# Returns the first element in the linked list
# @return [Object, nil] the first element or nil if the list is empty
def head
@list.first
end
# Returns the last element in the linked list
# @return [Object, nil] the last element or nil if the list is empty
def tail
@list.last
end
# Converts the linked list to an array
# @return [Array] the array representation of the linked list
def to_array
@list
end
# Converts the linked list to a string representation
# @return [String] the string representation of the linked list
def to_string
"[" + @list.join(",") + "]"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment