当前位置:网站首页>*List reversal
*List reversal
2022-07-27 16:25:00 【Cute rain】
Title Description : Define a function , Enter the head node of a linked list , Invert the linked list and output the head node of the inverted linked list
// Head node definition
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};Ideas : For linked list inversion , The most common method is to insert and reverse the nodes all the time next 了 , These two methods have two cases respectively , The linked list has head nodes and the linked list has no head nodes . Straight in : Straight head insertion is to remove the nodes of the original linked list from front to back, and then reconnect to the head node , Because the first inserted node is always behind the linked list , So after re inserting the head , The linked list is inverted .
Method 1 : Always insert the diagram ( Headless node ):

Code implementation
ListNode* reverseList(ListNode* head) {
ListNode* p = head;
ListNode* s = NULL;
head = NULL;
while (p != NULL)
{
s = p;
p = p->next;
s->next = head;
head = s;
}
return head;
}There are head nodes :

Code implementation :
ListNode* Reverse(ListNode* p)
{
assert(p != NULL);
if (p == NULL && p->next == NULL)
{
return p;
}
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
head->next = NULL;
while (p != NULL)
{
ListNode* s = p;
p = p->next;
s->next = head->next;
head->next = s;
}
p = head->next;
free(head);
return p;
}
Method 2 : Reverse the list of next Domain diagram ( Headless node ):

Code implementation :
ListNode* reversePrint(ListNode* head)
{
ListNode *p=NULL;
ListNode *q=head->next;
ListNode *r=head->next;
head->next=NULL;
while(r->next!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}
return head;
}Header node code implementation :
ListNode* reversePrint(ListNode* head)
{
ListNode *p=NULL;
ListNode *q=head->next->next;
ListNode *r=head->next->next;
head->next->next=NULL;
head->next=NULL;
while(r->next!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}
head->next=q;
return head->next;
}Method 3 : Recursive inversion :
ListNode* RevList(ListNode* pre, ListNode* p)
{
ListNode* tail = NULL;
if (p != NULL)
{
tail = RevList(p, p->next);
p->next = pre;
}
else
{
tail = pre;
}
return tail;
}
边栏推荐
- JSP基础
- Axure 安装图标字体元件库
- MapReduce instance (III): data De duplication
- 2021-06-02
- 4位数的随机数据
- Taking advantage of 5g Dongfeng, does MediaTek want to fight the high-end market again?
- Pychart imports the existing local installation package
- IO stream introduction
- Time series - use tsfresh for classification tasks
- EXE程序加密锁
猜你喜欢
随机推荐
excel skill
DRF use: get request to get data (small example)
Nacos
Draw circuit diagram according to Verilog code
Oracle 常用语句
大数相加
jupyter 创建虚拟环境并安装pytorch(gpu)
插入word中的图片保持高dpi方法
It can carry 100 people! Musk releases the strongest "starship" in history! Go to Mars as early as next year!
A powerful web vulnerability scanning and verification tool (vulmap)
Test novice learning classic (with ideas)
: 0xc0000005: an access conflict occurs when writing position 0x01458000 - to be solved
Analysis of PHP keyword replacement classes (avoid repeated replacement, keep and restore the original links)
Cubemx联合IAR工程移植
Flask connects to existing tables in MySQL database
Determine the exact type of data
Servlet基础知识点
编码技巧——全局日志开关
The difference and use between get request and post request
指针总结









