[Leetcode] Reverse Linked List Inverted linked list

Source: Internet
Author: User

Reverse a singly linked list.

Click to show more hints.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

Before doing Reverse Linked List II inverted linked list of the time I also wonder how only two no one, the original really forgot Ah, now add, this problem with the previous way compared to a lot of simple, the topic in order to add some difficulty, let us use iterative and recursive return to achieve, but the difficulty is not big. We first look at the iterative solution, the idea is to create a dummy node before the original linked list, because the first node will change, and then start from the head, after the next node moved to dummy node, repeat this operation until the head is a stub point, the code is as follows:

Solution One

//iterativeclassSolution { Public: ListNode* Reverselist (listnode*head) {        if(!head)returnHead; ListNode*dummy =NewListNode (-1); Dummy->next =Head; ListNode*cur =Head;  while(cur->next) {ListNode*tmp = cur->Next; Cur->next = tmp->Next; TMP->next = dummy->Next; Dummy->next =tmp; }        returnDummy->Next; }};

Here we look at the recursive solution, the code is less, the idea of recursive solution is to continue to enter the recursive function, until the head point to the last node, p points to the previous node, and then change the position of Head and P, and then return to the previous layer of recursive function, and then Exchange P and head position, after each exchange, The head node is exchanged in the order of good, until the first node of P, and then exchange, the head node becomes the node, at this time the entire list also completed the flip, the code is as follows:

Solution Two

// Recursive class Solution {public:    listnode* reverselist (listnode* head) {        if return head;         *p = head;         = Reverselist (p->next);        P->next->next = p;        P->next = NULL;         return head;    }};

[Leetcode] Reverse Linked List Inverted 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.