[Data structure] connection operations of two single-cycle linked lists

Source: Internet
Author: User

If you operate this operation on a single-linked table or a linked list indicated by a head pointer, it will consume performance because you need to traverse the first linked list, locate an, and link b1 to an. the time complexity is: O (n ). If it is implemented on the single-loop linked list indicated by the tail pointer, you only need to change the pointer without traversing. the time complexity is: O (1)

The following four main steps are required for Algorithm Implementation:

LinkList Connect (LinkList A, LinkList B) {// assume that A and B are the end pointer LinkList p = A-> next; // 1. The position of the header node that stores the linked list is A-> next = B-> next; // 2. link the end of Table A to table B to free (B-> next) at the Start Node of expression B; // 3. Release B to express the header Node B-> next = P; // 4. Link next at the end of expression B to return B to the header of expression A; // return the tail pointer of the new cyclic linked list}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.