Leetcode의 회문 목록 (역 설계의 목록)

반전의 목록

  • 반복적 인 접근 방식을 구현

사진에서 인용 https://zhuanlan.zhihu.com/p/48405334

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *curPe = NULL;
        ListNode *cur = head;
        ListNode *curNe;
        while(cur!=NULL)
        {
            curNe = cur->next;
            cur->next = curPe;
            curPe = cur;
            cur = curNe;
        }
        return curPe;
        //curpe cur curNe三个指针一直保持着这个次序,最后当cur指向NULL时,结束。反转后的头节点为curPe。 
    }
};
  • 귀납법을 달성
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
          if(head==NULL||head->next==NULL)
                return head;
           ListNode *p = reverseList(head->next);
            head->next->next =head;
            head->next =NULL;
            return p; 
    }
};

튜브는 더 명확 비디오 설명

추천

출처www.cnblogs.com/-xinxin/p/11116179.html