示例#1
0
    // Time Complexity: O(N)
    // Space Complexity: O(1)

    public ListNode GetIntersection(ListNode headA, ListNode headB)
    {
        if (runnerA == null || runnerB == null)
        {
            return(null);
        }
        ListNode runnerA = headA;
        listNode runnerB = headB;
        int      lengthA = 0;
        int      lengthB = 0;

        while (runnerA != null)
        {
            ++lengthA;
            runnerA = runnerA.next;
        }
        while (runnerB != null)
        {
            ++lengthB;
            runnerB = runnerB.next;
        }
        runnerA = headA;
        runnerB = headB;
        int diff = lengthA - lengthB;

        while (diff > 0)
        {
            runnerA = runnerA.next;
            --diff;
        }
        while (diff < 0)
        {
            runnerB = runnerB.next;
            ++diff;
        }
        while (runnerA.next != null && runnerB.next != null)
        {
            if (runnerA = runnerB)
            {
                return(runnerA);
            }
            runnerA = runnerA.next;
            runnerB = runnerB.next;
        }
        return(null);
    }
示例#2
0
            public node ReadFromStream(Stream stream)
            {
                var dt = (DataType)stream.ReadByte();
                var ts = dt & DataType.TypeSpecMask;

                dt &= DataType.Mask;

                var hasRefId = false;
                var ix       = Array.IndexOf(_withRefs, dt);

                if (ix >= 0)
                {
                    hasRefId = true;
                    dt       = _reffables[ix];
                }

                node     node;
                DataType keyType = 0;

                switch (dt)
                {
                case DataType.End: return(null);

                case DataType.Byte: node = new valueNode {
                        Value = (byte)stream.ReadByte()
                }; break;

                case DataType.SByte: node = new valueNode {
                        Value = (sbyte)(byte)stream.ReadByte()
                }; break;

                case DataType.Int16: node = new valueNode {
                        Value = BitConverter.ToInt16(stream.Read(2), 0)
                }; break;

                case DataType.UInt16: node = new valueNode {
                        Value = BitConverter.ToUInt16(stream.Read(2), 0)
                }; break;

                case DataType.Int32: node = new valueNode {
                        Value = BitConverter.ToInt32(stream.Read(4), 0)
                }; break;

                case DataType.UInt32: node = new valueNode {
                        Value = BitConverter.ToUInt32(stream.Read(4), 0)
                }; break;

                case DataType.Single: node = new valueNode {
                        Value = BitConverter.ToSingle(stream.Read(4), 0)
                }; break;

                case DataType.Int64: node = new valueNode {
                        Value = BitConverter.ToInt64(stream.Read(8), 0)
                }; break;

                case DataType.UInt64: node = new valueNode {
                        Value = BitConverter.ToUInt64(stream.Read(8), 0)
                }; break;

                case DataType.Double: node = new valueNode {
                        Value = BitConverter.ToDouble(stream.Read(8), 0)
                }; break;

                case DataType.DateTime: node = new valueNode {
                        Value = DateTime.FromBinary(BitConverter.ToInt64(stream.Read(8), 0))
                }; break;

                case DataType.Decimal: node = new valueNode {
                        Value = stream.ReadDecimalOptim()
                }; break;

                case DataType.String: node = new valueNode {
                        Value = readBuffer(stream, true).FromUtf8()
                }; break;

                case DataType.RawData: node = new valueNode {
                        Value = readBuffer(stream, false)
                }; break;

                case DataType.Null: node = new valueNode {
                        Value = null
                }; break;

                case DataType.False: node = new valueNode {
                        Value = false
                }; break;

                case DataType.True: node = new valueNode {
                        Value = true
                }; break;

                case DataType.Ref: node = new valueNode {
                        Value = stream.ReadInt32Optim()
                }; break;

                case DataType.KeyValuePair: node = new kvpNode {
                        Key = ReadFromStream(stream), Value = ReadFromStream(stream)
                }; break;

                case DataType.List:
                    var list = new List <node>();
                    while ((node = ReadFromStream(stream)) != null)
                    {
                        list.Add(node);
                    }
                    node = new listNode {
                        List = list
                    };
                    break;

                case DataType.DictionaryOther:
                    keyType = (DataType)stream.ReadByte();
                    goto case DataType.DictionaryInt64;

                case DataType.DictionaryInt64:
                case DataType.DictionaryString:
                case DataType.DictionaryTwoStrings:
                    var dict = new Dictionary <object, node>();
                    // Dictionaries encode value first, then key.
                    while ((node = ReadFromStream(stream)) != null)
                    {
                        object key;
                        switch (dt)
                        {
                        case DataType.DictionaryInt64:
                            key = stream.ReadInt64Optim();
                            break;

                        case DataType.DictionaryString:
                            key = readBuffer(stream, true).FromUtf8();
                            break;

                        case DataType.DictionaryTwoStrings:
                            var fieldName     = readBuffer(stream, true).FromUtf8();
                            var declaringType = readBuffer(stream, true).FromUtf8();
                            key = declaringType.Length > 0 ? new FieldNameWithType(fieldName, declaringType) : (object)fieldName;
                            break;

                        case DataType.DictionaryOther:
                            switch (keyType)
                            {
                            case DataType.UInt64:
                                key = stream.ReadUInt64Optim();
                                break;

                            case DataType.Single:
                                key = BitConverter.ToSingle(stream.Read(4), 0);
                                break;

                            case DataType.Double:
                                key = BitConverter.ToSingle(stream.Read(8), 0);
                                break;

                            case DataType.DateTime:
                                key = DateTime.FromBinary(BitConverter.ToInt64(stream.Read(8), 0));
                                break;

                            case DataType.Decimal:
                                key = stream.ReadDecimalOptim();
                                break;

                            case DataType.RawData:
                                key = readBuffer(stream, false).FromUtf16();
                                break;

                            default:
                                throw new InvalidOperationException("Invalid dictionary key type.");
                            }
                            break;

                        default:
                            throw new InvalidOperationException("This case should never be reached.");
                        }
                        dict[key] = node;
                    }
                    node = new dictNode {
                        Dictionary = dict, KeyType = keyType
                    };
                    break;

                default:
                    throw new InvalidOperationException("Invalid object type in binary format.");
                }

                node.DataType = dt;
                if (ts == DataType.SimpleTypeSpec || (node.TypeSpecIsFull = (ts == DataType.FullTypeSpec)))
                {
                    node.TypeSpec = readBuffer(stream, true).FromUtf8();
                }
                if (hasRefId)
                {
                    node.RefId = stream.ReadInt32Optim();
                }
                return(node);
            }