zoukankan      html  css  js  c++  java
  • <LeetCode OJ> 234. Palindrome Linked List

    Total Accepted: 40445 Total Submissions: 148124 Difficulty: Easy

    Given a singly linked list, determine if it is a palindrome.

    Follow up:
    Could you do it in O(n) time and O(1) space?


    分析:

    临时没想到空间为O(1)的方法,直白的模拟思想,遍历一遍记录各节点的元素

    再对数组前尾比較就可以。

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        bool isPalindrome(ListNode* head) {
            ListNode* pNode=head;
            if(pNode==NULL)
                return true;
            vector<int> vec;
            while(pNode)
            {
                vec.push_back(pNode->val);
                pNode=pNode->next;
            }
            for(int i=0;i<vec.size()/2;i++)
            {
                if(vec[i]!=vec[vec.size()-1-i])
                    return false;
            }
            return true;
        }
    };


    学习别人的算法:原地推断

    My solution has three parts.

    1. Halve the list into two lists.将链表分为两个
    2. Reverse one of the sub lists.逆置当中一个
    3. Compare比較两链表

    举例:
    1->3->3->1,
    分成两半:1->3,3->1
    逆置后者:   1->3,1->3
    比較两者:1=1,3=3, return true;

    bool isPalindrome(ListNode* head) {
    if(!head) return true;
    
    ListNode* slow = head;
    ListNode* fast = slow->next;
    
    // 分成两半
    while(fast && fast->next && fast->next->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    
    ListNode* head2 = slow->next;
    slow->next = NULL;
    
    ListNode* prev = NULL;
    ListNode* cur = head2;
    ListNode* next = NULL;
    
    // 逆置
    while(head2) {
        next = head2->next;
        head2->next = prev;
        prev = head2;
        head2 = next;
    }
    
     // 比較
    head2 = prev;
    while(head && head2) {
        if(head->val != head2->val)
            return false;
    
        head = head->next;
        head2 = head2->next;
    }


    学习别人的就地逆置单链表

    单链表的反转问题是一道非常主要的问题。题目例如以下:
    有一个单链表:1 ->2->3->4->5->6 

    反转后链表为:6->5->4->3->2->1.

    解析:能够使用三个指针pre 。temp,next对逐个节点进行反转。

    具体流程例如以下: 


    (1) 初始状态
    pre = head; 

    tmp = head->next; 

    pre->next = null;//逆置

    (2)第一次循环:

    next = tmp->next; 
    tmp->next = pre;//逆置


    pre 和 tmp 后移一位 , 第一次循环结束,第一个节点指向了头节点。

    pre = tmp; 
    tmp = next;
    
    

    (3) 第二次循环

    next = tmp->next; 
    tmp->next = pre;

    单链表

    pre = tmp; 
    tmp = next; 

    链表反转

    (4)如此循环下去,直到最后一个节点被反转。


    小结:
    1)利用三个指针,pre,cur。next,遍历链表,遍历的而过程改变他们的指向
    pre的作用,cur指针所要反指回来的节点
    cur的作用。操作当前节点进行逆置
    next的作用,始终先跑到下一个将要逆置的节点位置
    
    
    
    

    注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!

    原文地址:http://blog.csdn.net/ebowtang/article/details/50531380

    原作者博客:http://blog.csdn.net/ebowtang


    參考资源:

    【1】http://www.cfanz.cn/index.php?c=article&a=read&id=280529


  • 相关阅读:
    二进制位运算
    Leetcode 373. Find K Pairs with Smallest Sums
    priority_queue的用法
    Leetcode 110. Balanced Binary Tree
    Leetcode 104. Maximum Depth of Binary Tree
    Leetcode 111. Minimum Depth of Binary Tree
    Leetcode 64. Minimum Path Sum
    Leetcode 63. Unique Paths II
    经典的递归练习
    案例:java中的基本排序
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7294476.html
Copyright © 2011-2022 走看看