LinkedList

LinkedList in Swift

How to rotate linked list with K-th places ?

[Question]: Given the head of a linked list, rotate the list to the right by k places.Example: – Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] #Approach Step: 1 Create tail node which moves till endsStep: 2 Create curr the tail node is the (len-k)-th node (1st node is head)Step: 3 Reorder linked list

How to create deep copy of linked list who has random pointer

[Question]: Create a deep copy of the linked list. which consist of exactly n fresh new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent …

How to create deep copy of linked list who has random pointer Read More »

How to Sort Linked List ?

[Question]: Given Head of singly linked list return a sorted linkedlist head(ascending order.)Criteria: -TC should O(n) SC: O(1) Input: head = [4,2,1,3] Output: [1,2,3,4] #Approach : We can use merge sort: which work on divide & conquer