Leetcode Reverse Linked list II reverse linked List 2

Source: Internet
Author: User

Test instructions: Returns the linked list of the specified segment [M,n], returning the chain header.

Idea: The main trouble in the chain table head, if you want to start from the linked list, more special.

At present, with the implementation of DFS, first find the location of the m-1, and then find the location of n+1, the middle of this paragraph on whether to reverse, to the DFS to solve, with a counter to count the number of the reverse has been.

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7  * };8  */9 classSolution {Ten  Public: Onelistnode* DFS (listnode* t,listnode* far,intnum) A     { -         if(num==0)returnFar ; -listnode* tmp=t->Next; thet->next=Far ; -         returnDFS (tmp, T, num-1); -     } -  +listnode* Reversebetween (listnode* head,intMintN) { -         if(m==n)returnhead; +listnode* T=head, *r=head; A  at          for(intI=0; i<n; i++) r=r->next;//find the first n+1 -         if(m==1)returnDFS (Head, R, n-m+1); -  -          for(intI=1; i<m-1; i++) t=t->next;//find the first m-1 -T->next=dfs (T->next, R, n-m+1); -         returnhead; in     } -};
AC Code

Leetcode Reverse Linked list II reverse linked List 2

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.