# My fairly short Java solution :) [Feel free to let me know how I can improve on it!]

• ``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {
//hashset can be used as an alternative solution here. store headA values and then traverse headB to match.
return null;
//assumption: linked list is sorted (asc).

if(valA == valB)

valA = startWith.val;
valB = oppNode.val;

while(startWith != null){
valA = startWith.val;
if(valA == valB){
return startWith;
} else if(valA > valB){
if(oppNode.next != null){
oppNode = oppNode.next;
valB = oppNode.val;
} else
return null;
} else if(valA < valB){
startWith = startWith.next;
}
}

return null;
}
}
``````

I believe that this solution runs at O(n) time and uses approximately O(1) space.

Cheers.

• Hello Suan, I am David. Just for a first greet with my solution to this problem, feel free to share your ideas!

``````    public class Solution {
public int getLength(ListNode node) {
int length = 0;
while (node != null) {
node = node.next;
length++;
}

return length;
}

int distance = Math.abs(lengthA - lengthB);
if (lengthA > lengthB) {
while (distance > 0) {
nodeA = nodeA.next;
distance--;
}
} else {
while (distance > 0) {
nodeB = nodeB.next;
distance--;
}
}

while (nodeA != null && nodeB != null) {
if (nodeA == nodeB) return nodeA;
nodeA = nodeA.next;
nodeB = nodeB.next;
}

return null;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.