LeetCode – 234. 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?

The difficulty of this question lies in the follow-up.

/** * Definition for singly-linked list. * public class ListNode { * int val; while(head != null){ ListNode n = new ListNode(head.val); n.next = result; result = n; head = head.next; } return result; } public boolean checksame(ListNode a, ListNode b){ while(a != null && b != null){ if(a.val != b.val){ return false; } a = a.next; b = b.next; } return true; } }

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:「LeetCode – 234. Palindrome Linked List」

211
0 0 211

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top