如何用C语言实现算法练习帖71中的链表反转?
- 内容介绍
- 文章标签
- 相关推荐
本文共计530个文字,预计阅读时间需要3分钟。
python反转链表一、题目描述定义一个函数,输入一个链表的头节点,反转该链表,并输出反转后链表的头节点。
题目来源:扣官网输入一个链表的头节点,反转该链表,并输出反转后链表的头节点。
示例:输入:1->2->3->4->5输出:5->4->3->2->1
class ListNode: def __init__(self, val=0, next=None): self.val=val self.next=next
def reverse_linked_list(head): prev=None current=head while current: next_node=current.next current.next=prev prev=current current=next_node return prev
测试代码if __name__==__main__: # 创建链表:1->2->3->4->5 head=ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5))))) # 反转链表 new_head=reverse_linked_list(head) # 输出反转后的链表 while new_head: print(new_head.val, end=->) new_head=new_head.next print(None)
反转链表一、题目描述定义一个函数输入一个链表的头节点反转该链表并输出反转后链表的头节点。题目来源力扣官网输入一个链表的头节点反转该链表并输出反转后链表的头节点。 题目来源力扣官网示例:输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL
限制0 < 节点个数 < 5000
二、解决方法
1.头插法
struct ListNode* reverseList(struct ListNode* head){if(headNULL||head->nextNULL){//判断链表长度若为0或1则直接返回headreturn head;}struct ListNode* temphead->next;//临时中间结点head->nextNULL;//头结点初始指向NULLwhile(temp!NULL){struct ListNode* curtemp;//获取当前结点 temptemp->next;//获取下一结点cur->nexthead;//将头结点拼接在当前结点之后headcur;//赋值新的头结点}return head;}
2.迭代法官方题解
struct ListNode* reverseList(struct ListNode* head){struct ListNode* preNULL;//前一个结点struct ListNode* curhead;//当前结点while(cur){//当前结点不为NULL时struct ListNode* nextcur->next;//下一节点//三结点更新cur->nextpre;precur;curnext;}return pre;}
3.递归法(官方题解)
struct ListNode* reverseList(struct ListNode* head) {if (head NULL || head->next NULL) {//递归中止条件return head;}struct ListNode* newHead reverseList(head->next);//新头结点head->next->next head;//反转当前head结点和head->next的指向head->next NULL;//将head->next断链防止成环return newHead;}
本文共计530个文字,预计阅读时间需要3分钟。
python反转链表一、题目描述定义一个函数,输入一个链表的头节点,反转该链表,并输出反转后链表的头节点。
题目来源:扣官网输入一个链表的头节点,反转该链表,并输出反转后链表的头节点。
示例:输入:1->2->3->4->5输出:5->4->3->2->1
class ListNode: def __init__(self, val=0, next=None): self.val=val self.next=next
def reverse_linked_list(head): prev=None current=head while current: next_node=current.next current.next=prev prev=current current=next_node return prev
测试代码if __name__==__main__: # 创建链表:1->2->3->4->5 head=ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5))))) # 反转链表 new_head=reverse_linked_list(head) # 输出反转后的链表 while new_head: print(new_head.val, end=->) new_head=new_head.next print(None)
反转链表一、题目描述定义一个函数输入一个链表的头节点反转该链表并输出反转后链表的头节点。题目来源力扣官网输入一个链表的头节点反转该链表并输出反转后链表的头节点。 题目来源力扣官网示例:输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL
限制0 < 节点个数 < 5000
二、解决方法
1.头插法
struct ListNode* reverseList(struct ListNode* head){if(headNULL||head->nextNULL){//判断链表长度若为0或1则直接返回headreturn head;}struct ListNode* temphead->next;//临时中间结点head->nextNULL;//头结点初始指向NULLwhile(temp!NULL){struct ListNode* curtemp;//获取当前结点 temptemp->next;//获取下一结点cur->nexthead;//将头结点拼接在当前结点之后headcur;//赋值新的头结点}return head;}
2.迭代法官方题解
struct ListNode* reverseList(struct ListNode* head){struct ListNode* preNULL;//前一个结点struct ListNode* curhead;//当前结点while(cur){//当前结点不为NULL时struct ListNode* nextcur->next;//下一节点//三结点更新cur->nextpre;precur;curnext;}return pre;}
3.递归法(官方题解)
struct ListNode* reverseList(struct ListNode* head) {if (head NULL || head->next NULL) {//递归中止条件return head;}struct ListNode* newHead reverseList(head->next);//新头结点head->next->next head;//反转当前head结点和head->next的指向head->next NULL;//将head->next断链防止成环return newHead;}

