single-linked list in reverse order
1. Single-linked list with chain header
consider a single linked list that requires reverse order:
then the list after the reverse order is:
Process:
(1) Take P1 point to Header->next (P1=stu->next);p2 reserved P1->next (p2=p1->next);P1->next is set to NULL because the current P1 node is the tail node after the reverse of the single-linked list p1->next=null;
(2) Take P3 to retain P2->next (P3=p2->next);before inserting the P2 into p1 (p2->next = p1);P1 points to the node (P1=P2) to which the P2 points;P2 points to the node (P2=P3) to which the P3 points;
the single linked list after the loop is modified is as follows:
(3) Repeat steps (2)
the single linked list after the loop is modified is as follows:
(4) Header->next point to P1, complete the reverse order of the entire single-linked list
typedef struct student{ int number; Char name[20]; int score; struct student *next;} Student;student *reverse (student *stu) { student *p1,*p2,*p3; if (stu = = NULL | | Stu->next = = NULL) return stu; p1=stu->next; P1 point to the next node of the node of the linked header p2=p1->next; p1->next=null; while (p2) { p3=p2->next; P2->next = p1; P1=P2; p2=p3; } stu->next=p1; return Stu;}
2. List Palindrome determine whether a one-way linked list is a palindrome list, requiring the time complexity of O (n) and the space Complexity of O (1). The algorithm has the following types:
- Traversing the entire list, the value of each node in the list is recorded in the array, and then determine whether the array is a palindrome array, time complexity is O (n), but the space complexity is O (n), does not meet the requirements of space complexity.
- Using the advanced nature of the stack, the first half of the chain list is pressed into the stack, and then a pop-up with the second half of the list. Time complexity O (n), but still requires n/2 of the stack space, the space complexity of O (n).
- Reverse the Chain list method, the second half of the list is flipped in place, then the first half, the second half of the second part of the comparison, to determine whether it is equal, time complexity O (n), the space Complexity of O (1) to meet the requirements of the topic.
the code that uses the fast and slow pointer stack is as follows:
Public Booleanchkpalindrome (ListNode A) {Stack<Integer> stack =NewStack<integer>(); if(A = =NULL|| A.next = =NULL) return true; ListNode Quick=A; ListNode Slow=A; BooleanFlag =false; while(Quick! =NULL) {stack.add (slow.val); Slow=Slow.next; Quick=Quick.next; if(Quick! =NULL) {Quick=Quick.next; } Else{flag=true; } } if(Flag = =true) Stack.pop (); while(!stack.isempty () && Slow! =NULL) { intVA =Stack.pop (); if(VA = =slow.val) Slow=Slow.next; Else return false; } if(Slow = =NULL&&stack.isempty ())return true; Else return false; }
View Codethe code for using the fast and slow pointers and flipping in place is as follows:
Public Booleanchkpalindrome (ListNode A) {if(A = =NULL) return false; if(A.next = =NULL) return true; ListNode Quick=A; ListNode Slow=A; while(Quick! =NULL&& Quick.next! =NULL) {Quick=Quick.next.next; Slow=Slow.next; } ListNode P=Slow.next; ListNode P1=P.next; while(P! =NULL) {P.next=slow; Slow=p; P=P1; if(P1! =NULL) {P1=P1.next; } } while(A! =slow) { if(A.val! =slow.val) {return false; } if(a.next==slow) { return true; } A=A.next; Slow=Slow.next; } return true; }
View Code
List in reverse order + determine whether the list is palindrome