How would I go about implementing a function in C++ that swaps the front node and tNode with each other in a singly linked list that has more than two nodes, but I can't swap data? Also what would the Big-O complexity be for this?
/*If you have any query do comment in the comment section else like the solution*/
1. Traverse the linked list, and store address of nodes in three
variable firstNode, secondLastNode and tNode.
2. Now follow below operation
tNode->next = firstNode->next
secondLastNode->next = firstNode
firstNode->next = null
head = tNode
return head
Since you have to traverse the complete list once, the time complexity will be O(n)
Get Answers For Free
Most questions answered within 1 hours.