<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>linked-list on seth-shi</title><link>https://sethix.com/tags/linked-list/</link><description>Recent content in linked-list on seth-shi</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Mon, 18 Feb 2019 15:43:12 +0800</lastBuildDate><atom:link href="https://sethix.com/tags/linked-list/index.xml" rel="self" type="application/rss+xml"/><item><title>Reverse Linked List - Recursive Approach</title><link>https://sethix.com/posts/2019021815/</link><pubDate>Mon, 18 Feb 2019 15:43:12 +0800</pubDate><guid>https://sethix.com/posts/2019021815/</guid><description>Recently encountered a linked list reversal problem on LeetCode:
Reverse a singly linked list. Example: Input: 1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;5-&amp;gt;NULL Output: 5-&amp;gt;4-&amp;gt;3-&amp;gt;2-&amp;gt;1-&amp;gt;NULL Iterative Solution /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode* head) { struct ListNode *tmpHead = head, *nextNode; if (NULL == head) { return head; } while (NULL != head-&amp;gt;next) { // Move the next node of current &amp;#39;head&amp;#39; to real head position nextNode = head-&amp;gt;next; head-&amp;gt;next = head-&amp;gt;next-&amp;gt;next; nextNode-&amp;gt;next = tmpHead; tmpHead = nextNode; } return tmpHead; } Recursive Solution /** * Definition for singly-linked list.</description></item><item><title>Implementation of Linked List in PHP</title><link>https://sethix.com/posts/2018042410/</link><pubDate>Tue, 24 Apr 2018 10:08:57 +0800</pubDate><guid>https://sethix.com/posts/2018042410/</guid><description>Starting to learn data structures
Today I changed the code font I use. Previously I used console which looked good, but today I found a more preferable style Source Code Pro.
Here are two screenshots - it&amp;rsquo;s quite pleasing!
Source Code Pro Source Code Pro Now let&amp;rsquo;s get to the main topic: linked list operations
Node Class First we need a Node class to store data &amp;lt;?php namespace LinkedList; class Node { /** * @var $data integer */ public $data; /** * Pointer to next node * * @var $next Node */ public $next; public function __construct(int $data = null) { // Initialize data assignment, can also be assigned via $node-&amp;gt;data = X; $this-&amp;gt;data = $data; } } Linked List Manager Class (For node operations) We&amp;rsquo;ll break down the manager class code into sections due to its length Head Insertion As the name suggests, insert at the head If list is empty, initialize with current node If not empty, make new node the head public function insertHead(int $data) : bool { $newNode = new Node($data); $newNode-&amp;gt;next = $this-&amp;gt;head; $this-&amp;gt;head = $newNode; return true; } Insert at Position (index=0 is head, returns false for invalid positions) public function insert(int $index = 0, int $data) : bool { if (is_null($this-&amp;gt;head) || $index === 0) { return $this-&amp;gt;insertHead($data); } $currNode = $this-&amp;gt;head; $startIndex = 1; for ($currIndex = $startIndex; !</description></item></channel></rss>