Singly Linked List
Search
- Python
def search(head, x):
curr = head
i = 0
while curr:
if curr.val == x:
return i
i += 1
curr = curr.next
return -1
Insert at beginning
- Python
def insertAtBeginning(head, x):
t = Node(x)
t.next = head
return t
Insert at end
- Python
def insertAtEnd(head, x):
if not head:
return Node(x)
curr = head
while curr.next:
curr = curr.next
curr.next = Node(x)
return head
Insert at index
- Python
def insertAtIndex(head, x, p):
t = Node(x)
if p == 0:
t.next = head
return t
curr = head
for i in range(p - 2):
curr = curr.next
if not curr:
return head
t.next = curr.next
curr.next = t
return head
Delete first
- Python
def delFirst(head):
if head:
return head.next
return None
Delete end
- Python
def delEnd(head):
if head == None or head.next == None:
return None
curr = head
while curr.next.next:
curr = curr.next
curr.next = None
return head