Move the last element to front in a linked list

June 23, 2022 · 1 min read

https://www.geeksforgeeks.org/move-last-element-to-front-of-a-given-linked-list/

void moveToFront(Node **head) {
  if (*head == nullptr || (*head)->next == nullptr)
    return;

  Node *prev = nullptr;
  Node *tmp = *head;

  while (tmp->next != nullptr) {
    prev = tmp;
    tmp = tmp->next;
  }

  prev->next = nullptr;
  tmp->next = *head;
  *head = tmp;
}
Remove duplicates in a unsorted linked list
Add 1 to a number represented as a linked list