leetcode_206 title--reverse Linked list (linked list)

Source: Internet
Author: User

Reverse Linked ListTotal accepted:1726 Total submissions:4378my submissions QuestionSolution

Reverse a singly linked list.

Click to show more hints.

Hide TagsLinked ListHas you met this question in a real interview? Yes No

Discuss

This problem is relatively simple, mainly to the list of reverse order, so you can set three pointers, Ptr0,ptr1 point to the two nodes to be exchanged, PTR2 before the exchange point to ptr1 the next node,

This is good for the next exchange to prepare, that is, to move back on it, until the end

#include <iostream>using namespace std;struct listnode {int val; ListNode *next; ListNode (int x): Val (x), Next (NULL) {}}; listnode* reverselist (listnode* head) {if (head==null| | Head->next==null) return head; listnode* Ptr0=head; listnode* ptr1=head->next; listnode* Ptr2;ptr0->next=null;while (1) {ptr2=ptr1->next;ptr1->next=ptr0;if (Ptr2==NULL) break;ptr0=ptr1; PTR1=PTR2;} return PTR1;} int main () {}

  

leetcode_206 title--reverse Linked list (linked list)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.