Fortune left France title 7 basis class3- reverse the one-way and two-way linked list
Problem 1: Reverse singly linked list
[Required] If the chain length is N, the time complexity requirements of O (N), additional space
complexity requirements of O (1)
1. Analyze
Inversion may be used singly linked list iterative method, it requires three pointers, respectively a front node records, the current node and a node. When reversing the current node point before a node, then the current node and the previous node backward, to continue until the end of reversal. Because after reversal after the current node point to change the node will be disconnected, so it is necessary to hold a pointer to a node of the current node. The overall condition of the iteration is the current node is not empty.
2. The core code
(1) produce a complete list
① as data structures, val data field, * next point to the next node, since the node pointed to, so as to define a pointer type ListNode.
typedef struct ListNode
{
int val;
ListNode *next;
}*List;
Tail using interpolation ②: The new node is inserted at the end
malloc is responsible for opening up a space for storage space size is sizeof(ListNode)
due to return to the default malloc void *, need to cast to List type structure pointer. L is a head node for storing data not only for storing the address of the head of the list, pointing to the tail node temporarily head node. P open node stores data, the tail node to the new node configuration list and then the new node as the tail node, and then to continue to develop new storage node. Finally, the tail node to null.
ListNode* L = (List)malloc(sizeof(ListNode));
//List L = (List)malloc(sizeof(ListNode));与上式等价
L->next = NULL;
//使用尾插法
List p,r;//p是新结点,r是尾节点
r = L; //把尾节点指向头节点
for(int i = 0;i < N;i++)
{
p = (List)malloc(sizeof(ListNode));
p->val = i * 5;
r->next = p;
r = p;
}
r->next = NULL;
(2) single chain reversal
List reverseList(List head)
{
List pre = NULL;
List cur = head->next;//需要反转的当前节点从头节点的下一个开始
while(cur != NULL)
{
List temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
3. The complete code
#include<iostream>
#include<stdlib.h>
//#include<time.h>
#define N 5
typedef struct ListNode
{
int val;
ListNode *next;
}*List;
List reverseList(List head)
{
List pre = NULL;
List cur = head->next;
while(cur != NULL)
{
List temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
int main()
{
//srand(time(0));
ListNode* L = (List)malloc(sizeof(ListNode));
//List L = (List)malloc(sizeof(ListNode));与上式等价
L->next = NULL;
//使用尾插法
List p,r;//p是新结点,r是尾节点
r = L; //把尾节点指向头节点
for(int i = 0;i < N;i++)
{
p = (List)malloc(sizeof(ListNode));
//p->val = rand() % 15;
p->val = i * 5;
r->next = p;
r = p;
}
r->next = NULL;
List newhead = NULL;
newhead = reverseList(L) ;
system("pause");
return 0;
}
4. The output
L is the original way linked list, and val is the first head node;
way linked list after inversion.
Topic 2: Reverse doubly linked list
Reverse the one-way and two-way linked list
[title], respectively, to achieve a one-way linked list reversal function and reverse doubly linked list.
[Required] If the chain length is N, the time complexity requirements of O (N), additional space
complexity requirements of O (1)
1. Analyze
Doubly linked list just need to reverse pre pointer and the next pointer of the current node to the exchange, a separate operation for each node, and not other nodes.
2. The core code
Creating (1) a doubly linked list
of doubly linked list data structure consists of a pointer field data two fields;
typedef struct doublelist
{
int val;
doublelist* next;
doublelist* pre;
}*List;
Doubly linked list tail interpolation and substantially the same way except that the step of recording the increased pre pointer;
List p,r,head = NULL;
r = NULL;
for(int i = 0;i < 4;i++)
{
p = (List)malloc(sizeof(doublelist));
p->val = 5 * i;
p->pre = r;
if(r == NULL)
{
r = p;
head = p;
continue;
}
r->next = p;
r = p;
}
r->next =NULL;
(2) Reverse doubly linked list
using the next pointer intermediate temporary variable, and then exchange pre next pointer, the last pointer to the previous node is assigned to the current node, continued reverse. Note that, the next pointer as pointer already pre exchanged, so the first pointer to a node actually is the next node.
List reverseList(List head)
{
List p = head;
List temp = NULL;
while(p!=NULL)
{
temp = p->next;
p->next = p->pre;
p->pre = temp;
p = p->pre;
}
return head;
}
3. The complete code
#include<iostream>
typedef struct doublelist
{
int val;
doublelist* next;
doublelist* pre;
}*List;
List reverseList(List head)
{
List p = head;
List temp = NULL;
while(p!=NULL)
{
temp = p->next;
p->next = p->pre;
p->pre = temp;
p = p->pre;
}
return head;
}
int main()
{
List p,r,head = NULL;
r = NULL;
for(int i = 0;i < 4;i++)
{
p = (List)malloc(sizeof(doublelist));
p->val = 5 * i;
p->pre = r;
if(r == NULL)
{
r = p;
head = p;
continue;
}
r->next = p;
r = p;
}
r->next =NULL;
List newhead = NULL;
newhead = reverseList(head) ;
return 0;
}
4. The output
When you create a linked list followed by the next direction 0,5,10,15, and pre direction is empty;
after reversing direction next list is empty, while pre direction 0,5,10,15, it can be seen already doubly linked list reverse.