160. 相交链表

160. 相交链表

160. 相交链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        global null
        null=''
        head1=headA
        head2=headB

        while head1!=head2:
            if head1:
                head1=head1.next
            else:
                head1=headB
            if head2:
                head2=head2.next
            else:
                head2=headA
        return head1
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null||headB==null)
 
        {
            return null;
        }
        ListNode head1=headA;
        ListNode head2=headB;
        while(head1!=head2){
            if(head1!=null){
                head1=head1.next;
            }else{
                head1=headB;
            }
            if(head2!=null){
                head2=head2.next;
            }else{
                head2=headA;
            }
           
        } 
        return head1;

    }
        
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {

        if(headA==NULL||headB==NULL)
 
        {
            return NULL;
        }
        struct ListNode *head1=headA;
        struct ListNode *head2=headB;
        while(head1!=head2){
            if(head1!=NULL){
                head1=head1->next;
            }else{
                head1=headB;
            }
            if(head2!=NULL){
                head2=head2->next;
            }else{
                head2=headA;
            }
           
        } 
        return head1;      
}
上一篇:sql 存储时"空格"转成问号,且这个空格不是真的空格


下一篇:leetcode-160. 相交链表(链表)