Practice Linked List

Review of LeetCode

  • Daily practice
  • Have a break
  • Revisit a problem and solve again

Review of Linked List

class Node:
  def __init__(self, x):
    self.val = x
    self.next = None

Hackerrank problems

def printLinkedList(head):
    tmp = head
    while tmp:
        print(tmp.data)
        tmp = tmp.next

Insert a node at the head of a linked list

def insertNodeAtHead(llist, data):
    # Write your code here
    new_node = SinglyLinkedList()
    new_node.data = data
    new_node.next = None

    if not llist:
        llist = new_node
        return llist
    new_node.next = llist
    llist = new_node
    return llist

Reverse a linked list

def reverse(head):
    cur, prev, next = head, None, None
    while cur:
        next = cur.next
        cur.next = prev
        prev = cur
        cur = next
    return prev

Practice

Compare two linked list

def compare_lists(llist1, llist2):
    if not llist1 and not llist2:
        return True
    while llist1 or llist2:
        if not llist1 or not llist2:
            return False
        if llist1.data != llist2.data:
            return False
        llist1 = llist1.next ; llist2 = llist2.next
    return True

Solutions

- https://leetcode.com/problems/middle-of-the-linked-list
Approach 1: Find the length
class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        def findLength(head):
            cnt = 0
            cur = head
            while cur:
                cnt += 1
                cur = cur.next
            return cnt
        
        cur = head
        mid = findLength(head) // 2
        while mid > 0:
            cur = cur.next
            mid -= 1
        return cur
Approach 2: Turtle and Rabbit (Tortoise and Hare)
class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow
Written on August 4, 2019