Circular link List (4)-Split list is two segments

Source: Internet
Author: User

The following example shows how to split a linked list. Use code to implement it.
                                      The original circular linked list  


                                     Segmented loop sub-list 1  


                                    Segmented loop sub-list 2  


1) using the algorithm of stepping 1 and 2 respectively, the middle pointer and the tail pointer of the linked list are obtained;
2) The second part of the linked list form a circular chain list;
3) The first half of the linked list forms a circular chain list;
4) Set the head pointer of the two loop list.


in the following implementations, if the number of nodes in the list is odd, the first half of the list will be 1 more than the number of the second half of the list.
Code implementation:
#include <iostream>//linked list node struct node{int data; Node *next;};/ /Divide a list (head) into two linked lists. Head1 and Head2 represent the two linked lists that were split. void Splitlist (node *head, node **head1, node **head2) {node *slowptr = head; Node *fastptr = head;if (head = = NULL) return;//If the number of nodes in the loop list is odd, then the final fastptr->next will coincide with the head//If the number is even, then fastptr->next- >next will coincide with head while (fastptr->next! = Head &&fastptr->next->next! = head) {fastptr = fastptr-> Next->next;slowptr = Slowptr->next;} If there is an even number of nodes, move fastptrif (Fastptr->next->next = = head) Fastptr = fastptr->next;//Set the first half of the head pointer *head1 = head;// Set the second half of the head pointer if (head->next! = head) *head2 = slowptr->next;//make the back part of the loop list Fastptr->next = slowptr->next;// Make the first half of the loop linked list slowptr->next = head;} Insert a new node in the loop list header void push (node **head, int data) {Node *newnode = new node; Node *temp = *head;newnode->data = Data;newnode->next = *head;//If the linked list is not empty, the last node's back pointer is set to the new node//That is, the new node becomes the new head node. if (*head! = NULL) {while (Temp->next! = *head) temp = Temp->next;temp->next = NewNode;} Elsenewnode->next = NewNode;  The new node is made as the first node of the list *head = NewNode; Adjust head node}//print loop linked list void Printlist (node *head) {Node *temp = head;if (head! = NULL) {do{std::cout << "" << Temp-&gt ;d ata << ""; temp = temp->next;} while (temp! = head);}} int main () {//Initialize linked list: 1->2->3->4->5->6->7node *head = NULL; Node *head1 = NULL; Node *head2 = Null;push (&head, 7);p Ush (&head, 6);p Ush (&head, 5);p Ush (&head, 4);p Ush (&head, 3);p Ush (&head, 2);p Ush (&head, 1); Std::cout << "Original Circular Linked List \ n";p rintlist (head); Std::cout <& Lt std::endl;//Split list splitlist (head, &head1, &head2) std::cout << "\nfirst Circular Linked List \ n";p rintlist (HEAD1); Std::cout << std::endl;std::cout << "\nsecond Circular Linked List \ n";p rintlist (head2); Std::cout & lt;< Std::endl;return 0;}
Output:
Original Circular Linked List
1 2 3 4 5 6 7

First Circular Linked List
1 2 3 4

Second Circular Linked List
5 6 7

time Complexity: O (n)

Circular link List (4)-Split list is two segments

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.