leetcode_Palindrome Linked List

描述:

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?

思路:

1.如何判断一个链表是否是回文的?很简单将链表中的元素遍历出来并放进ArrayList中,然后可以像数组一样来判断该元素是否为回文的,时间复杂度O(n),空间复杂度O(n),可如何用O(n)的时间复杂度和O(1)的空间复杂度来解决呢?

2.是不是可以考虑 将链表反转?可反转后还是链表啊,要是将链表分为前后两个部分呢,分为两个部分还是无法判断该链表是否为回文链表啊,那要是再将其中一个链表反转一下呢,It‘s done!好多时候,多想一步容易,再多想一步就困难了。

代码:

public boolean isPalindrome(ListNode head) {
        if(head==null||head.next==null)
            return true;
        ListNode p=head,temp=head,quick=head;
        while(temp!=null&&quick!=null)
        {
            temp=temp.next;
            if(quick.next==null)
                break;
            quick=quick.next.next;
        }
        temp=reverseList(temp);
        p=head;
        while(temp!=null&&p!=null)
        {
            if(temp.val!=p.val)
                return false;
            temp=temp.next;
            p=p.next;
        }
        return true;
    }
    public ListNode reverseList(ListNode head)
    {
        ListNode tempHead=new ListNode(-1);
        ListNode p=head.next,q=null;
        tempHead.next=head;
        head.next=null;
        while(p!=null)
        {
            q=p;
            p=p.next;
            q.next=tempHead.next;
            tempHead.next=q;
        }
        return tempHead.next;
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-30 00:21:00

leetcode_Palindrome Linked List的相关文章

Palindrome Linked List Leetcode

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? 这个follow up要求O(n)的时间和O(1)的空间,可以先reverse一半,然后再对比.只是reverse的时候要考虑奇数个还是偶数个.如果是奇数个的话,就跳过最中间的,从下一个开始reverse. /** * Definition for singly-li

237. Delete Node in a Linked List

1. 问题描述 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 ->

Reverse Linked List

题目: Reverse a singly linked list. cpp: class Solution { public: ListNode* reverseList(ListNode* head) { if(!head || !head->next) return head; ListNode *p1 = head; ListNode *p2 = head->next; ListNode *p3 = head->next->next; p1->next = nullpt

Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

[leedcode 142] Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!).  我们已经得到了结论a=c,那么让两个指针分别从X

Palindrome Linked List

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? 思路: 把链表一分为二,把右边的一半翻转,再逐个比对左右的链表即可. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo

Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 class Solution { public: TreeNode *node = NULL; void flatten(TreeNode *root) { if(root == NULL) return; i

[C++]LeetCode: 60 Intersection of Two Linked Lists

题目: Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

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 ≤ le