【leetcode】92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

 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 class Solution {
10 public:
11     ListNode* reverseBetween(ListNode* head, int m, int n) {
12         int d=n-m;
13         if(head==NULL||head->next==NULL||d<=0)
14             return head;
15
16         ListNode* p=head,*bfp=p;
17         ListNode* q=head;
18         for(int i=0;i<m-1;i++){
19             bfp=p;
20             p=p->next;
21         }
22         ListNode* revhead=p;
23         ListNode* bf=p,*bh=NULL;
24         p=p->next;
25         d--;
26         while(p->next!=NULL&&d!=0){
27             d--;
28             bh=p->next;
29             p->next=bf;
30             bf=p;
31             p=bh;
32
33         }
34         bh=p->next;
35         p->next=bf;
36         bfp->next=p;
37         revhead->next=bh;
38         if(m!=1)
39             return head;
40         else return p;
41     }
42 };
时间: 2024-12-09 19:32:33

【leetcode】92. Reverse Linked List II的相关文章

【Leetcode】92. Reverse Linked List II &amp;&amp; 206. Reverse Linked List

The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three pointers to achieve this goal. 1) Pointer to last value 2) Pointer to cur p value 3) Pointer to next value Here, showing my code wishes can help u. #incl

LeetCode OJ 92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【LeetCode】206 - Reverse Linked List

Reverse a singly linked list. Hint:A linked list can be reversed either iteratively or recursively. Could you implement both? Solution 1:iteration 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 *

【LeetCode】119 - Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? Solution: 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex){

【leetcode】Pascal&#39;s Triangle I &amp; II (middle)

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 思路:杨辉三角,直接按规律生成即可 vector<vector<int> > generate(int numRows) { vector<vector<int>>

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

【Leetcode】Pascal&amp;#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路:最简单的方法就是依照[Leetcode]Pascal's Triangle 的方式自顶向下依次求解,但会造成空间的浪费.若仅仅用一个vect

【LeetCode】Evaluate Reverse Polish Notation

题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"]

【leetcode】998. Maximum Binary Tree II

题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree. Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with