linked list and classic problems

In preparing for the coding interview, I will start writing blogs to record the hints and tips about the basic data structures. The series of the blog will start from Linked List in this one.

Definition:

According to the book, Cracking the Coding Interview, a linked list is a data structure that represents a sequence of nodes where each node stores a value and points to next node.

diagram

diagram

Unlike array, linked list didn’t allow constant time access to a particular “index” within the list. When you have to find the (k)-th element in the list, you have to iterate through k elements.

The advantage of using Linked List is that items could be added or removed from the head in constant time.

A Linked List Node

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
  
    def appendToTail(self, x):
        end = ListNode(x)   # declare an end node
        while self.next:    # iterate to the current ending node
          self = self.next
        self.next = end     # let the current ending node point to the new ending node
if __name__ == '__main__':
    A = ListNode(1)
    A.appendToTail(2)
    A.appendToTail(3)
    A.appendToTail(4)
    
    print(A.val)    # 1st element
    print(A.next.val)   # 2nd element
    print(A.next.next.val)    # 3rd element
    print(A.next.next.next.val)   # 4th element
    
## 1
## 2
## 3
## 4

Some Basic Manipulation of Linked List

1. Remove node from Linked List

You are given a linked list with much more than one element and an integer Then the task is to write a function that is able to remove the node whose stored value match the given integer.

Test the value stored in the next node, when the value matched replace the next node by the more next one.

# remove the node from linked list
class Solution:
    def removeNode(self, head, value):
        if head.val == value:
            return head.next
        tmp = head
        while tmp.next:
            if tmp.next.val == value:
                tmp.next = tmp.next.next
            else:
                tmp = tmp.next
        return head
if __name__ == '__main__':
    A = ListNode(2)
    A.appendToTail(2)
    A.appendToTail(1)
    ans = Solution().removeNode(A, 1)
    print(ans.next.next)  # None: 1 has been removed
## None

4.(Advanced) Delete the node in the middle

You are given one Linked List, build a function to delete the node in the middle of the list

class Solution:
    def deleteMiddle(self, head):
        index = 0
        current = head
        middle = head
        pre = ListNode(None)
        pre.next = head
        while current.next:
            index += 1
            if index > 0 and index % 2 == 0:
                middle = middle.next
                pre = pre.next
            current = current.next
        pre.next = pre.next.next
        return head
if __name__ == '__main__':
    A = ListNode(1)
    A.appendToTail(2)
    A.appendToTail(1)
    ans = Solution().deleteMiddle(A)
    print(A.val)        # 1
    print(A.next.val)   # 1
## 1
## 1

See also