class ListNode:
    def __init__(self, val=None):
        self.val = val = None

Traversal Linked Lists

Floyd’s cycle-finding algorithm

141. Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Thought process:

  • Use two pointers each traversal the linked list at different ‘speed’.
  • If there is a cycle, the faster pointer will eventually intersect the slower pointer.
def has_cycle(head):
    fast, slow = head, head
    while fast and
        fast =
        slow =
        if fast == slow: return True
    return False

142. Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Thought process:

  • First detect if cycle exists or not.
  • Traversal one pointer from head and one pointer from the intersect at the same speed.
  • They will intersect at the begining of the cycle.
def detect_cycle(head):
    def has_cycle(head):
        fast, slow = head, head
        while fast and
            fast =
            slow =
            if fast == slow: return slow
        return None

    intersect = has_cycle(head)
    if not intersect: return None
    node = head
    while node != intersect:
        node =
        intersect =
    return intersect

160. Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.

Thought process:

  • The problem looks like this:
B:   BB
     start of intersection
  • Traversal the two lists at the same speed, when one reaches the end, jump the pointer to the head of the other one.
  • They will reach each other at the intersection node.
        start of intersection
def get_intersection_node(head_a, head_b):
    trav1, trav2 = head_a, head_b
    while trav1 != trav2:
        if not trav1: trav1 = head_b
        else: trav1 =

        if not trav2: trav2 = head_a
        else: trav2 =
    return trav1

Manipulate Linked Lists

203. Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.

Thought process:

  • Iterate through the linked list, when we see the next node is of the target value, short that link.
def remove_elements(head, val):
    ret = node = ListNode(None) = head
    while node and
        if == val: =
        else: node =

19. Remove Nth Node From End of List

Given a linked list, remove the n-th node from the end of list and return its head.

Thought process:

  • Use two pointers to traversal the linked list.
  • Make them n + 1 nodes apart and advancing at same speed.
  • When the right one reaches the end, the left one is pointing at the nth node from the ned
def remove_nth_from_end(head, n):
    ret = left = ListNode(None) = head

    right = head
    while right and n:
        right =
        n -= 1

    while right:
        right =
        left = =

206. Reverse Linked List

Reverse a singly linked list.

Thought process:

None     1 --> 2 --> None
 ^       ^
 |       |
prev curr=head
  • Have two pointers, pointing at the previous and current node.
  • We want the current node points backwards, have move the two pointers forward.
  • Onliner is prev,, curr = curr, prev, which is a bit tricky.
  • Or have a temp variable to be safer.
def reverse_list(head):
    prev, curr = None, head
    while curr: prev,, curr = curr, prev,
    return prev

92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in one-pass.

Thought process:

  • Move to the mth node. Keep a reference of the (m-1)th node, it has to point at the head of the reversed sub list.
  • Reverse the sublist with n - m + 1 nodes.
  • Connect the (m-1)th and (n+1)th node to the reversed sublist.
  • Remeber to handle the edge case is when m = 0.
def reverse_between(head, m, n):
    prev, trav = None, head
    while m > 1:
        prev, trav = trav,
        m, n = m - 1, n - 1

    tail, last = trav, prev

    while n:
        prev,, trav = trav, prev,
        n -= 1

    if last: = prev
        head = prev = trav
    return head

369. Plus One Linked List

Given a non-negative integer represented as a linked list of digits, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list. ```python class Solution: def plusOne(self, head: ListNode) -> ListNode: def reverse(head): prev, curr = None, head while curr: nxt = = prev prev = curr curr = nxt return prev

    head = reverse(head)

    curr, carry = head, 1
    while curr:
        carry, curr.val = divmod(curr.val + carry, 10)
        if carry and not
   = ListNode()
        curr =

    return reverse(head) ``` Or iterative without reverse ```python class Solution:
def plusOne(self, head: ListNode) -> ListNode:
    sentinel = slow = ListNode(val=0, next=head)
    fast = head
    while fast:
        if fast.val != 9:
            slow = fast
        fast =
    slow.val += 1
    while = 0
        slow =
    return sentinel if sentinel.val == 1 else ```

24. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

# swap
prev -> curr -> next -> beyond
prev -> next -> curr -> beyond
                prev -> curr -> next -> beyond
class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        sentinel = ListNode(None, next=head)
        prev, curr = sentinel,
        while curr and # has a pair of nodes to swap
            next_ =
            beyond =
  ,, = next_, beyond, curr
            prev, curr = curr,

This is a special case of the next question.

25. Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

Thought process:

  • Iterativly reverse sublist of size k. Keep track of the head and tail of the reversed sublist.
  • Have the previous tail points to he new head, then update the pointer of previous tail.
  • Link the remaining nodes in the end.
def reverse_k_group(head, k):
    def has_k_nodes(node, k):
        while node and k:
            node =
            k -= 1
        return k == 0

    def reverse_k_nodes(node, k):
        trav, prev, tail = node, None, node
        for i in range(k):
            prev,, trav = trav, prev,
        return trav, prev, tail

    ret = prev_tail = ListNode(None) = node = head
    while has_k_nodes(node, k):
        node, this_head, this_tail = reverse_k_nodes(node, k) = this_head
        prev_tail = this_tail = node

234. Palindrome Linked List

Given a singly linked list, determine if it is a palindrome.

Thought process:

  • Find the middle of the linked list.
  • Reverse the second half.
  • Compare nodes one by one between the first half and reversed second half.
  • Be a nice person and undo the reverse on the second half.
def isPalindrome(head):
    if not head or not return True
    def get_midpoint(head):
        slow, fast = head, head
        while fast and
            slow =
            fast =
        return slow

    def reverse(head):
        prev, trav = None, head
        while trav:
            prev,, trav = trav, prev,
        return prev

    mid = reverse(get_midpoint(head))
    p, q = head, mid
    while p and q and p != q:
        if p.val != q.val:
            return False
            p =
            q =
    return True

328. Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

Thought process:

  • Alternativly grab values from the list and put into the right list.
def odd_even_list(head):
    even_head = even = ListNode(None)
    odd_head = odd = ListNode(None)

    odd_turn = True
    while head:
        if odd_turn:
   = head
            odd =
   = head
            even =

        head =
        odd_turn = not odd_turn = None =

143. Reorder List

Given a singly linked list \(L: L_0 → L_1 → … → L_{n-1} → L_n\) reorder it to: \(L_0 → L_n → L_1 → L_{n-1} → L_2 → L_{n-2} → …\)

Thought process:

  • 1->2->3->4->5 will becomes 1->5->2->4->3
  • So basically break the list into two halves(second half be one node less when we have odd number of nodes), reverse the second half, and stitch them together.
def reorder_list(head):
    def split(head):
        l1 = l2 = head
        while l2 and
            l1 =
            l2 =
        middle = = None
        return middle

    def reverse(head):
        prev, trav = None, head
        while trav:
            prev,, trav = trav, prev,
        return prev

    if not head or not return head

    l1 = head
    l2 = reverse(split(head))
    ret = trav = ListNode(None)
    while l1 and l2: = l1
        l1 =
        trav = = l2
        l2 =
        trav =

    if l1: = l1
    return ret

138. Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.

Thought process:

  • If we can use hash map to keep a one to one mapping between original nodes and their copies. We can add the random pointers easily. With this method we probably need to do two pass, once to make copies and handle the regular next pointers between the copies, a second time to add the random pointers.
  • To optimize space, we won’t use hashmap, so we need to use the pointers we have to keep track of things.
  • We can do three passes.
    1. We make copies and insert them directly after original node. So the is the copy and is the orignal next node.
    2. We add the random pointers between the copies with the fact that copy.random should be
    3. We de-couple the intermingled original and copied linked list.
def copy_random_list(head):
    node = head
    while node:
        copy = Node(node.val) = = copy
        node =

    node = head
    while node:
        copy =
        if not node.random: copy.random = None
        else: copy.random =
        node =

    node = head
    ret = copy = None if not head else
    while node: =
        node = = None if not node else
        copy =
    return ret

708. Insert into a Sorted Circular Linked List

Given a node from a Circular Linked List which is sorted in ascending order, write a function to insert a value insertVal into the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list, and may not be necessarily the smallest value in the circular list.

Thought process:

  • Find the insert location and insert it.
  • Its either in the middle of the cycle if the value is within bound or where the cycle starts if the value is outside the bound.
def insert(head, val):
    if not head:
        nd = ListNode(val) = nd
        return nd

    prev, trav = head,
    while trav != head:
        if prev.val <= val <= trav.val:
        elif prev.val > trav.val:
            if val >= prev.val or val <= trav.val:
        prev, trav = trav, = ListNode(val, trav)
    return head

Merge Lists

21. Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Thought process:

  • Traversal the two lists with two pointers.
  • Iterativly add the smaller of the two nodes to the list and advance the respective pointer.
  • Add the remainder to the end.
def merge_two_sorted_lists(l1, l2):
    ret = trav = ListNode(None)
    while l1 and l2:
        if l1.val < l2.val:
   = l1
            l1 =
   = l2
            l2 =
        trav = = l1 if l1 else l2

23. Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Thought process:

  • Merge lists two at a time from bottom up.
  • With k lists at the bottom, each level up, the number of lists is halved.
  • There \(log_2^K\) levels in total, each level the amount of work is \(N\), that is the overall number of nodes.
  • So the time complexity is O(Nlogk).
def merge_k_lists(self, lists: List[ListNode]) -> ListNode:
    ret = trav = ListNode(0)
    q = PriorityQueue()

    for node in lists:
        q.put((node.val, id(node), node))

    while not q.empty():
        val, _, node = q.get() = ListNode(val)
        trav =
        node =
        if node: q.put((node.val, id(node), node))

Small note on id is need to compare the case where node.val are equal, since node object is not comparable.

Small note on PriorityQueue from queue, it functionally ok, but more heavy weight than heapq in python.

2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Thought process:

  • Do math one digit at a time, carry over overflows.
def add_two_numbers(l1, l2):
    carry = 0
    ret = trav = ListNode(None)

    while l1 or l2 or carry:
        if l1:
            carry += l1.val
            l1 =
        if l2:
            carry += l2.val
            l2 =
        carry, val = divmod(carry, 10) = ListNode(val)
        trav =

445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Thought process:

  • reverse the two lists.
  • add them use the code above.
  • then reverse the result.
    def add_two_numbers_ii(l1, l2):
        l1 = reverse_list(l1)
        l2 = reverse_list(l2)
        l3 = add_two_numbers(l1, l2)
        l3 = reverse_list(l3)
        return l3

1634. Add Two Polynomials Represented as Linked Lists

A polynomial linked list is a special type of linked list where every node represents a term in a polynomial expression. Each node has three attributes:

  1. coefficient: an integer representing the number multiplier of the term. The coefficient of the term 9x4 is 9.
  2. power: an integer representing the exponent. The power of the term 9x4 is 4.
  3. next: a pointer to the next node in the list, or null if it is the last node of the list. For example, the polynomial 5x3 + 4x - 7 is represented by the polynomial linked list illustrated below: img
    The polynomial linked list must be in its standard form: the polynomial must be in strictly descending order by its power value. Also, terms with a coefficient of 0 are omitted.
    Given two polynomial linked list heads, poly1 and poly2, add the polynomials together and return the head of the sum of the polynomials. PolyNode format: The input/output format is as a list of n nodes, where each node is represented as its [coefficient, power]. For example, the polynomial 5x3 + 4x - 7 would be represented as: [[5,3],[4,1],[-7,0]]. ```python class Solution: def addPoly(self, poly1: ‘PolyNode’, poly2: ‘PolyNode’) -> ‘PolyNode’: curr = sentinel = PolyNode()
    while poly1 and poly2:
        if poly1.power > poly2.power:
   = PolyNode(poly1.coefficient, poly1.power)
            curr =
            poly1 =
        elif poly2.power > poly1.power:
   = PolyNode(poly2.coefficient, poly2.power)
            curr =
            poly2 =
            coef = poly1.coefficient + poly2.coefficient
            if coef != 0:
       = PolyNode(coef, poly1.power)
                curr =
            poly1 =
            poly2 =   = None
    if poly1: = poly1
    if poly2: = poly2
    return ```


148. Sort List

Sort a linked list in O(n log n) time using constant space complexity.

Thought process:

  • Do bottom up merge sort.
class Solution:
    def sortList(self, head: ListNode) -> ListNode:
        if head is None: return None

        def count_nodes(node):
            """count the number of nodes in linked list starting with given node."""
            total = 0
            while node: total, node = total + 1,
            return total
        def split(node, length):
            """From given node, cut the link at given length. Return the next node after cut. 
            split(A, 1) will cut A->B and return B
            split(A, 2) will cut B->C and return C
            while length > 1 and node: length, node = length - 1,
            if node is None: return None
            temp, =, None
            return temp
        def merge(l, r, node):
            """Merge two linked list in sorted order. Return the tail."""
            while l and r:
                if l.val < r.val:, l = l,
                else:   , r = r,
                node =
   = l if l is not None else r
            while node =
            return node

        size = count_nodes(head)
        sentinel = ListNode(None, head)
        length = 1
        while length < size:
            node, tail =, sentinel
            while node:
                l, r = node, split(node, length)
                node = split(r, length)
                tail = merge(l, r, tail)
            length <<= 1

430. Flatten a Multilevel Doubly Linked List

class Solution:
    def flatten(self, head: 'Node') -> 'Node':
        if not head: return head
        prev = sentinel = Node(None, None, head, None)
        stack = [head]
        while stack:
            curr = stack.pop()
   = curr
            curr.prev = prev
            if stack.append(
            if curr.child: 
                curr.child = None
            prev = curr
            = None