• ``````class Solution {
public:
int lenA = 0;
int lenB = 0;
lenA++;
}
lenB++;
}
while(lenA>lenB){
lenA--;
}
while(lenA<lenB){
lenB--;
}
}
}
};
``````
1. get the length of A and B;
2. if length of A is bigger than length of B, then A is backward.
3. B is same as A
4. if length of A equals length of B, then compare

but if I use headA++ to replace headA->next, it will get TLE

• Its because head++ is not same as head->next. This can be explained from pointer arithmetic. To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer:ptr++
Now, after the above operation, the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer location which is 4 bytes next to the current location. This operation will move the pointer to next memory location without impacting actual value at the memory location. If ptr points to a character whose address is 1000, then above operation will point to the location 1001 because next character will be available at 1001.

Similarly let say head was pointing to address 1000, so after the operation head++, head will point to 1000+sizeof(ListNode). Which may or may not be address of the head->next. Because in linked list data structure each node may or may not be stored in contiguous memory.

Difference Between Array and Linked List