1, make the ring type linked list
2. Detect the presence of rings in the list (reference article)
3, calculate the length of the ring in the chain list
4, calculate the position of the ring starting in the chain list
5, determine whether a chain list is a palindrome, requires O (n) time and O (1) space complexity (to determine whether the linked list is a palindrome, request O (n) time, O (1) space)
6, calculate the middle position of the linked list
7, the list in situ reversal (linked list in situ reversal)
8, test code
#include <iostream>using namespace Std;/*@1:detect If there is a circule in a list--DETECT_CIRCULE_EXIST@2:CA Lculate the circule length in the list, give_circ_len@3:locate the place where the circule starts-locate_circ_ Place@4:detect If the list is palindrome, Time:o (n) and Space:o (1) *//*struct simple linked list*/struct item{int n;s TRUCT item *next;}; Class Iter {struct item *head;int circ_len;public:void Insert (int i); bool Remove (int i); Iter () {This->circ_len = 0;this ->head = new (struct item); this->head->next = NULL;} BOOL Create_circule (int i); void Remove_circule (); bool Detect_circule_exist (); int give_circ_len (); int locate_circ_ Place (); bool Detect_palindrome (); void display (); ~iter ();}; void Iter::insert (int i) {struct item *cur;struct Item *add_item = new (struct item); add_item->n = I;add_item->next = null;/* Insert at Tail */cur = This->head;while (cur->next) {cur = cur->next;} Cur->next = add_item;cout << add_item << Endl;} BOOL Iter::remove (int i) {struct Item *cur = this->head->next, *prev = this->head;while (cur) {if (cur->n = = i) {prev->next = Cur->next;return true;} else {cur = Cur->next;prev = Prev->next;}} return false;} void iter::d isplay () {struct Item *cur = this->head->next;while (cur) {cout << cur->n << "; cur = Cur->next;} cout << "End" << Endl;} /* Create a circult at value:i, if not I exist return false*/bool iter::create_circule (int i) {struct Item *cur = This-> ; head;struct Item *find = Null;while (cur->next) {cur = cur->next;if (cur->n = i) {find = cur;}} if (find) {cur->next = Find;return true;} Elsereturn false;} /* Detect if exist a circule in the linked list */bool iter:: Detect_circule_exist () {/*quick and Slow point to the first E Lement of the linked list*/struct item *quick, *slow, *mark;bool find = False;slow = This->head->next;quick = this-& Gt;head->next->next;while (Quick && slow) {IF (!find) {if (quick = = slow) {find = True;mark = Slow;slow = slow->next;this->circ_len++;} else {if (!quick->nex T | | !quick->next->next) break;if (!slow->next) Break;quick = Quick->next->next;slow = Slow->next;}} else {if (Mark = = slow) {return true;} else {slow = slow->next;this->circ_len++;}}} find = False;return find;} int iter:: Give_circ_len () {this->detect_circule_exist (); return this->circ_len;} /* Cal the Len of non circule at the linked list */int iter:: Locate_circ_place () {if (This->detect_circule_exist ()) {St RUCT Item *quick, *slow;int i = This->circ_len;slow = This->head->next;quick = This->head->next;while (qui CK && i--) {quick = Quick->next;} i = 0;while (quick && slow) {if (quick = = slow) {return i;} else {I++;quick = Quick->next;slow = slow->next; }}} else {return 0;}} void iter:: Remove_circule () {struct Item *tail = this->head;struct Item *mark;int i = This->locate_circ_place () + 1; while (TAIl->next && i--) {tail = Tail->next;} Mark = Tail;while (tail) {if (Tail->next = = Mark) {tail->next = Null;return;} else {tail = Tail->next;}}} BOOL iter:: Detect_palindrome () {struct item *quick, *slow;/*first find the middle place of the linked list by quick and SL ow pointer*/slow = This->head->next;quick = This->head->next->next;while (slow) {struct Item *mark;slow = Slow->next;quick = Quick->next->next;if (!quick) {/*reverse The linked list at the Place*/mark = Slow;quick = Slo w->next->next; /*q*/slow = slow->next; /*P*/STRUCT Item *temp;while (quick) {temp = Quick->next;quick->next = Slow;slow = Quick;quick = temp;} Mark->next->next = Null;mark->next = Slow;slow = This->head->next;mark = Mark->next;while (Slow && Amp Mark) {//cout << odd First part: << slow->n << ' addr: ' << slow << ' second part: ' < < Mark->n << "addr:" << Mark <<endl;if (SloW->n! = mark->n) return false;else {slow = Slow->next;mark = Mark->next;}} return true;} if (!quick->next) {/*reverse The linked list at the Place*/mark = Slow;quick = slow->next->next;/*q*/slow = slow ->next; /*P*/STRUCT Item *temp;while (quick) {temp = Quick->next;quick->next = Slow;slow = Quick;quick = temp;} Mark->next->next = Null;mark->next = Slow;slow = This->head->next;mark = Mark->next;while (Slow && Amp Mark) {//cout << even first part: << slow->n << ' addr: ' << slow << ' second part: ' < ;< mark->n << "addr:" << Mark <<endl;if (slow->n! = mark->n) return false;else {slow = slow-& Gt;next;mark = Mark->next;}} return true;}}} Iter::~iter () {struct Item *cur = this->head->next;if (This->detect_circule_exist ()) This->remove_circule (); while (cur) {struct item *temp = Cur;cur = Cur->next;delete temp;//cout << "Delete" << temp<< Endl;} Delete THis->head;} int main () {class ITER myiter;/*for (int i = 0; i < ten; i++) {Myiter.insert (i);} Myiter.create_circule (2); if (Myiter.detect_circule_exist ()) cout << "detect circule exist and Circ Len" << Myiter.give_circ_len () << endl;elsecout << "No detect circule exist" <<endl; cout << "Circ point index" << myiter.locate_circ_place () <<endl; Myiter.remove_circule (); Myiter.display (); */* Myiter.insert (1); Myiter.insert (2); Myiter.insert (3); Myiter.insert (3); Myiter.insert (2); Myiter.insert (1); if (Myiter.detect_palindrome ()) cout << "even detect palindrome" <<endl; else cout << "even no detect palindrome" <<endl; */Myiter.insert (1); Myiter.insert (2); Myiter.insert (3); Myiter.insert (2); Myiter.insert (1); if (Myiter.detect_palindrome ()) cout << "Odd detect palindrome" <<endl; else cout << "Odd no Detect palindrome" <<endl; return 1;}
Fast and slow pointers and lists in-situ reversal