package demo.linkedlist;
* 链表结构
*/
class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public class JudgeLinkedListMerge {
public ListNode judgeLinkedListMerge(ListNode nodeM,ListNode nodeN ,int m, int n) {
ListNode shorterNode = null;
ListNode longerNode = null;
if(m>n) {
longerNode = nodeM;
shorterNode = nodeN;
} else {
longerNode = nodeN;
shorterNode = nodeM;
}
int diff = Math.abs(m-n);
while(diff>0) {
longerNode = longerNode.next;
diff--;
}
while(shorterNode != null) {
if(shorterNode.val == longerNode.val) {
if(judgeLinkedListMerge(shorterNode.next,longerNode.next)) {
return shorterNode;
}
} else {
shorterNode = shorterNode.next;
longerNode = longerNode.next;
}
}
return null;
}
* 节点是否相同
* @param nodeM
* @param nodeN
* @return
*/
public boolean judgeRecursion(ListNode nodeM,ListNode nodeN) {
if(nodeM!=null && nodeN!=null) {
if(nodeM.val == nodeN.val) {
return judgeLinkedListMerge(nodeM.next,nodeN.next);
} else {
return false;
}
} else {
return true;
}
}
}
评论