Move Last Element

Easy
0/40
Average time to solve is 15m
24 upvotes

Problem statement

You have been given a linked list. Write a function that moves the last element to the front in a given singly Linked List and returns the head of the new linked list.

For example, if the given Linked List is 5 -> 4 -> 3 -> 2 -> 1 -> NULL, then the function should return the head of the linked list 1 -> 5 -> 4 -> 3 -> 2 -> NULL.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer ‘T’, denoting the number of test cases. Then each test case follows.

The first line of each test case contains the Integer ‘N’ denoting the number of elements in the linked list. The first element is the head of the given linked list.

The second and the last line of each test case contains ‘N’  space-separated integers representing the node value of the linked list.
Output Format:
For each test case, print a single line containing ‘N’ single space-separated integers, the elements of the new linked list.

The output of each test case will be printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 5 * 10 ^ 3
-10 ^ 9 <= nodeVal[i] <= 10 ^ 9

Time Limit: 1 sec.
Sample Input 1:
2
5
5 4 3 2 1
1
5
Sample Output 1:
1 5 4 3 2
5
Explanation of Sample Input 1:
In the first test case, after moving the last element ‘1’ to first. The new linked list is  1 -> 5 -> 4 -> 3 -> 2 -> NULL.

In the second test case, it contains only one element, so the first element is the last element.
Sample Input 2:
1
4
-1 0 0 -1
Sample Output 2:
-1 -1 0 0 
Explanation of Sample Output 2:
In the first test case, after moving the last element ‘-1’ to first. The new linked list is -1 -> -1 ->0 -> 0 -> NULL.
Hint

Move a tail pointer to the second last element

Approaches (1)
Using Next and Tail Pointer To Change Link

The idea is to traverse the list till the last node using two pointers, one to store the address of the last node and the other to store the address of the second last node.

 

After traversing the list, make the following required link changes

  • Make the second last as last ( ‘secLast’ -> next  = NULL).
  • Set next to last as head (‘last’ -> next = ‘head’).
  • Make last as head ( ‘head’ = last)

 

Algorithm :

 

  • If ‘head’ is NULL or ‘head’ -> next is NULL, return ‘head’.
  • Take a pointer ‘secLast’ initializes it to NULL and a pointer ‘last’ initializes it to ‘head’.
  • Run a loop while ‘last’-> next is not NULL
    • Make ‘secLast’ = ‘last’.
    • Make ‘last’ = ‘last’ -> ‘next’
  • Set ‘secLast’ -> next to NULL
  • Set ‘last’ -> next to ‘head’
  • Make ‘head’ as ‘last’ i.e ‘head’ = ‘last’.
  • Return ‘head’
Time Complexity

O(N)   where ‘N’ is the number of nodes in the given linked list.

 

Since we are traversing the linked list once to get the ‘secLast’ and ‘last’ pointer at the required position. So the time complexity is O(‘N’). 

Space Complexity

 O(1)

 

We are using constant space, just a few pointers. So time complexity O(1).

Code Solution
(100% EXP penalty)
Move Last Element
Full screen
Console