{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 연결 리스트"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 페어의 노드 스왑"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/swap-nodes-in-pairs/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "연결 리스트를 입력받아 페어 단위로 스왑하라\n",
    "\n",
    "입력 : \n",
    "\n",
    "1->2->3->4\n",
    "\n",
    "출력 :\n",
    "\n",
    "2->1->4->3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ListNode:\n",
    "    def __init__(self, val=0, next=None):\n",
    "        self.val = val\n",
    "        self.next = next        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def printLinkedList(head: ListNode):\n",
    "    while head:\n",
    "        print(head.val)\n",
    "        head = head.next"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "node_4 = ListNode(4)\n",
    "node_3 = ListNode(3, node_4)\n",
    "node_2 = ListNode(2, node_3)\n",
    "node_1 = ListNode(1, node_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def swapPairs(self, head: ListNode) -> ListNode:\n",
    "        if head is None or head.next is None:\n",
    "            return head\n",
    "        cnt = 0\n",
    "        cur = head\n",
    "        nxt = head.next\n",
    "        while nxt:\n",
    "            if cnt == 0:\n",
    "                cur.val, nxt.val = nxt.val, cur.val\n",
    "                cnt += 1\n",
    "            else:\n",
    "                cnt = 0\n",
    "            cur = nxt\n",
    "            nxt = nxt.next\n",
    "        \n",
    "        return head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "solution = Solution()\n",
    "printLinkedList(solution.swapPairs(node_1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\tAccepted\t32 ms\t14.1 MB\tpython3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 풀이 1. 값만 교환"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def swapPairs(self, head: ListNode) -> ListNode:\n",
    "        cur = head\n",
    "        \n",
    "        while cur and cur.next:\n",
    "            cur.val, cur.next.val = cur.next.val, cur.val\n",
    "            cur = cur.next.next\n",
    "            \n",
    "        return head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "solution = Solution()\n",
    "printLinkedList(solution.swapPairs(node_1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\tAccepted\t32 ms\t14.3 MB\tpython3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "같은 방법을 간단하게 구현함..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 풀이 2. 반복 구조로 스왑"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def swapPairs(self, head: ListNode) -> ListNode:\n",
    "        root = prev = ListNode(None)\n",
    "        prev.next = head\n",
    "        \n",
    "        while head and head.next:\n",
    "            b = head.next\n",
    "            head.next = b.next\n",
    "            b.next = head\n",
    "            \n",
    "            prev.next = b\n",
    "            \n",
    "            head = head.next\n",
    "            prev = prev.next.next\n",
    "            \n",
    "        return root.next"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "solution = Solution()\n",
    "printLinkedList(solution.swapPairs(node_1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\tAccepted\t28 ms\t14.2 MB\tpython3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 풀이 3. 재귀 구조로 스왑"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def swapPairs(self, head: ListNode) -> ListNode:\n",
    "        if head and head.next:\n",
    "            p = head.next\n",
    "            \n",
    "            head.next = self.swapPairs(p.next)\n",
    "            p.next = head\n",
    "            return p\n",
    "        return head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "solution = Solution()\n",
    "printLinkedList(solution.swapPairs(node_1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Accepted\t28 ms\t14.1 MB\tpython3"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}