Skip to content

Instantly share code, notes, and snippets.

@Hamptonjc
Last active January 27, 2021 02:53
Show Gist options
  • Save Hamptonjc/e04028f108a0791f9f3c888b1c8a15b9 to your computer and use it in GitHub Desktop.
Save Hamptonjc/e04028f108a0791f9f3c888b1c8a15b9 to your computer and use it in GitHub Desktop.
reversing_linked_list.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "reversing_linked_list.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyPGeKSJu1pOsS5nm7h7JwyJ",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/Hamptonjc/e04028f108a0791f9f3c888b1c8a15b9/reversing_linked_list.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PiWLHgXFvPtq"
},
"source": [
"# Reversing a Linked List\n",
"*because these are real life skillz*"
]
},
{
"cell_type": "code",
"metadata": {
"id": "mBewLOtEpP0Z"
},
"source": [
"class LinkedList:\n",
"\n",
" class Node:\n",
" def __init__(self, value):\n",
" self.val = value\n",
" self.next = None\n",
"\n",
" def __init__(self, n_nodes):\n",
" self.head = self.create_list(n_nodes)\n",
" self.print_list()\n",
" \n",
" def create_list(self, n_nodes):\n",
" head = self.Node(1)\n",
" node_i = head\n",
" for i in range(2,n_nodes+1):\n",
" node_i.next = self.Node(i)\n",
" node_i = node_i.next\n",
" return head\n",
"\n",
" def print_list(self):\n",
" head = self.head\n",
" while head:\n",
" if head.next:\n",
" print(head.val, \"->\", end=\" \")\n",
" else:\n",
" print(head.val, end=\" \")\n",
" head = head.next\n",
"\n",
"\n",
" def reverse(self):\n",
" vals = []\n",
" node_i = self.head\n",
" while node_i:\n",
" vals.append(node_i.val)\n",
" node_i = node_i.next\n",
"\n",
" node_i = self.head\n",
" counter = len(vals)-1\n",
" while node_i:\n",
" node_i.val = vals[counter]\n",
" counter-=1\n",
" node_i = node_i.next\n",
"\n"
],
"execution_count": 34,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wcPXL9tGqbhP",
"outputId": "a58e3399-a0b5-436d-83a2-dd0a9e3f7b64"
},
"source": [
"LL1 = LinkedList(n_nodes=7)"
],
"execution_count": 35,
"outputs": [
{
"output_type": "stream",
"text": [
"1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 "
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "hp-_sSenrGJS"
},
"source": [
"LL1.reverse()"
],
"execution_count": 36,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TAZYzz_Mr1mb",
"outputId": "dc4d147c-d6df-49cc-bdc7-cca2e2a345fe"
},
"source": [
"LL1.print_list()"
],
"execution_count": 38,
"outputs": [
{
"output_type": "stream",
"text": [
"7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 "
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment