Linked List 2
Linked List 2
Linked-List 2
Now moving further with the topic, let’s try to solve some problems now...
1
Python Code
def printMiddle(headNode):
if headNode is None or headNode.next is None:
return head
slow = headNode #Slow pointer
fast = headNode.next #Fast Pointer
while (fast is not None and fast.next is not None):
fast = fast.next.next
slow = slow.next
return slow #Slow pointer shall point to our middle element
Note:
● For odd length there will be only one middle element, but for the even length
there will be two middle elements.
● In case of an even length LL, both these approaches, will return the f irst
middle element and the other one will be the direct n
ext o
f the first middle
element.
● We will be merging the linked list, similar to the way we performed merge
over two sorted arrays.
● We will be using the two h
ead pointers, compare their data and the one
found smaller will be directed to the new linked list, and increase the head
pointer of the corresponding linked list.
● Just remember to maintain the h
ead pointer separately for the new sorted
list.
● And also if one of the linked list’s length ends and the other one’s not, then
the remaining linked list will directly be appended to the final list.
● Try to implement this approach on your own.
2
● Like the merge sort algorithm is applied over the arrays, the same way we
will be applying it over the linked list.
● Just the difference is that in the case of arrays, the middle element could be
easily figured out, but here you have to find the middle element, each time
you send the linked list to split into two halves using the above approach.
● The merging part of the divided lists can also be done using the merge
sorted linked lists code a
s discussed above.
● The functionalities of this code have already been implemented by you, just
use them directly in your functions at the specified places.
● Try to implement this approach on your own.
Recursive approach:
● In this approach, we will store the last element of the list in the small answer,
and then update that by adding the next last node and so on.
● Finally, when we will be reaching the first element, we will assign the n
ext to
NONE.
● Follow the Python code below, for better understanding.
def reverseLinkedList(head) :
if head is None or head.next is None
return head
smallHead = reverseLinkedList(head.next)
head.next.next head
head.next = None
return smallHead
3
After calculation, you can see that this code has a time complexity of O
(n2). Now
let’s think about how to improve it.
def reverse2(head):
if head is None or head.next is None:
return head, head
smallHead,smallTail = reverse2(head.next)
smallTail.next= head
head.next = None
return smallHead,head
def reverse3(head):
if head is None or head.next is None:
return head
smallHead = reverse3(head.next)
tail = head.next
tail.next = head
head.next = None
return smallHead
Iterative approach:
4
def reverse(headNode):
if headNode==None:
return None
elif headNode.next==None:
return headNode
prev=headNode
curr=prev.next
next=curr.next
prev.next None
while next:
curr.next = prev
prev = curr
curr = next
next = next.next
curr.next = prev
return curr
Practice problems
Try over the following link to practice some good questions related to linked lists:
https://www.hackerrank.com/domains/data-structures?filters%5Bsubdomains%5D%5B%
5
5D=linked-lists
6