【Leetcode】92. Reverse Linked List II && 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.

  

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
}ListNode, *PNode;

void create_List(PNode head)
{
    PNode  p = head;
    int n;
    cin>>n;
    for(int i = 0; i < n ;i ++){
        int t;
        cin>>t;
        if(i == 0){
            head -> val = t;
            head -> next = NULL;
            cout<<"head is "<<head->val<<endl;
            p = head;
        }else{
            PNode newNode = (PNode) malloc(sizeof(PNode));
            newNode -> val = t;
            newNode -> next = NULL;
            p -> next = newNode;
            p = newNode;
            cout<<"p is "<<p -> val<<endl;
        }
    }
}

void display(PNode  head)
{
    PNode p = head;
    while(p){
        cout<<p->val<<" -> ";
        p = p -> next;
    }cout<<endl;
}

class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(m == n || head == NULL) return head;
        ListNode *pLast = head, *p = head -> next, *pNext = NULL;
        ListNode *newN = NULL, *newM = NULL, *beforeM = head, *afterN = NULL;
        int pos = 1;
        while(p){
            if(pos == m - 1){
                beforeM = pLast;
                pLast = p;
                p = p -> next;
            }
            else if(pos >= m && pos < n){
                pNext = p -> next;
                p -> next = pLast;
                if(pos == m){
                    pLast -> next = NULL;
                    newM = pLast;
                }
                pLast = p;
                if(pos == n - 1){
                    newN = p;
                    afterN = pNext;
                }
                p = pNext;
            }else{
                pLast = p;
                p = p -> next;
            }
            pos ++;
        }
        if( m==1 && afterN == NULL){
            head = newN;
        }else if(m == 1){
            head = newN;
            newM -> next = afterN;
        }else{
            beforeM -> next = newN;
            newM -> next = afterN;
        }
        return head;
    }

    ListNode* reverseList(ListNode* head) {
        if(head == NULL) return head;
        ListNode *pLast = head, *p = head -> next, *pNext = NULL;
        while(p){
            pNext = p -> next;
            p -> next = pLast;
            if(pLast == head){
                pLast -> next = NULL;
            }
            pLast = p;
            p = pNext;
        }
        head = pLast;
        return head;
    }
};
int main()
{
    PNode head = (PNode) malloc(sizeof(PNode));;
    create_List(head);
    cout<<"after creating , head is "<<head->val<<endl;
    display(head);
    Solution tmp = Solution();
    //tmp.reverseBetween(head, 2, 3);
    tmp.reverseList(head);
    system("pause");
    return 0;
}
时间: 2024-12-14 09:10:22

【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List的相关文章

【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】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】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

【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->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】Unique Binary Search Trees II

题目链接:https://leetcode.com/problems/unique-binary-search-trees-ii/ 题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below.

【Leetcode】Search a 2D Matrix II

题目链接:https://leetcode.com/problems/search-a-2d-matrix-ii/ 题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Intege

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's sh

【Leetcode】445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers