首页 > 试题广场 >

判断一个链表是否为回文链表,说出你的思路并手写代码

[问答题]

判断一个链表是否为回文链表,说出你的思路并手写代码

#include <iostream>
#include <string>
#include <stack>
using namespace std;

struct node {
    int value;
    node *next;
};

node *add_node(node *head, int value) {
    node *new_node = new node();
    new_node -> value = value;
    new_node -> next = NULL;
    node *p_node = new node();
    p_node = head;
    if(head == NULL)
        head = new_node;
    else {
        while(p_node -> next != NULL) {
            p_node = p_node -> next;
        }
        p_node -> next = new_node;
    }
    return head;
}

bool Reverse_jadge(node *head) {
    stack <node *> node_stack;
    node * p_node;
    p_node = head;
    int temp = 0;
    while(p_node != NULL) {
        node_stack.push(p_node);
        temp++;
        p_node = p_node -> next;
    }
    for (int i = 0; i <= temp / 2; i++) {
        if (node_stack.top() -> value == head -> value) {
            node_stack.pop();
            head = head -> next;
        }
        else
            return false;
    }
    return true;
}

int main() {
    node *head = NULL;
    string str, str_s;

    getline(cin, str);
    while(str.find(' ', 0) != string :: npos) {
        head = add_node(head, atoi(str.substr(0, str.find(' ', 0)).c_str()));
        str.erase(0, str.find(' ', 0) + 1);
    }
    head = add_node(head, atoi(str.c_str()));

    bool flag = Reverse_jadge(head);
    if(flag)
        cout <<  "Ture" << endl;
    else
        cout << "False" << endl;
    return 0;
}

发表于 2019-09-06 21:36:35 回复(0)
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if head is None or head.next is None:
            return True
        Len = 0
        p = head 
        while p:
            p = p.next
            Len += 1
        
        prev = head
        now = head.next
        
        prev.next = None
        for i in range(Len//2-1):
            tmp = now.next
            now.next = prev
            prev = now
            now = tmp
        
        p1 = prev
        p2 = now
        if Len%2==1:
            p2 = p2.next
        
        while p1 is not None and p2 is not None:
            if p1.val != p2.val:
                return False
            p1 = p1.next
            p2 = p2.next
            
        if p1 is not None or p2 is not None:
            return False
        return True

发表于 2019-08-06 14:51:50 回复(0)
/**
 * 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) {
        if(head == NULL)    return true;
        ListNode* slow = head,*fast = head,*p;
        while(fast && fast->next){
            slow = slow->next;
            fast = fast->next->next;
        }
        //reverse
        ListNode* pre = NULL,*cur = head,*next = head;
        while(cur != slow){
            next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        }

        slow = fast == NULL ?  slow : slow->next;
        while(slow && pre){
            if(slow->val != pre->val)   return false;
            slow = slow->next;
            pre = pre->next;
        }
        return true;
    }
};

发表于 2021-03-26 18:15:05 回复(0)
public class PalindromeList{     //找到中间节点
    public ListNode getMid(ListNode head){         ListNode fast=head;         ListNode slow=head;             while(fast!=null){             fast=fast.next;             if(fast==null){                 break;             }             slow=slow.next;             fast=fast.next;         }         return slow;
    }        //逆置从中间节点之后的链表
    public ListNode reverse(ListNode head){         ListNode result=null;         ListNode cur=head;         while(cur!=null){             ListNode next=cur.next;                     cur.next=result;             result=cur;                         cur=next;         }         return result;
    }      //然后判断相等
    public boolean checkPalindromeList(ListNode head){         ListNode mid=getMid(head);         ListNode node=reverse(mid);         ListNode p1=head;         ListNode p2=node;         while(p2!=null&&p1!=null){             if(p1.val!=p2,val){                 return false;             }             p1=p1.next;             p2=p2.next;         }         return true;
    }
}

发表于 2019-07-26 22:38:48 回复(0)