首页 热点专区 小学知识 中学知识 出国留学 考研考公
您的当前位置:首页正文

1-a. 链表逆序

来源:要发发知识网

(不可申请额外空间)

如图:


解题思路:

  1. 依次遍历链表结点,每遍历一个结点即逆置一个结点

  2. 遍历第一个结点:


  3. 遍历第二个结点:


4.遍历第三个结点:

5.遍历第四个结点:


  1. 遍历第五个结点:


具体操作过程:

  1. 备份head->next

  2. 修改head->next

  3. 修改headnew_head

具体代码:

ListNode* reverseList(ListNode* head)
{
    ListNode* new_head = NULL;
    while( head )
     {
          ListNode* head_next = head->next;   // 备份head->next
          head->next = new_head;      // 更新head->next
          new_head = head;            // 移动new->head
           head = head_next;
    }
            return new_head;      
}

完成代码 :

显示全文