Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse and compare. 另外一种非常经典的办法是用 Recursive 的思路,把一个list看成这种形式: 0 ( 1 ( 2 ( 3 ) 2 ) 1 ) 0 0 ( 1 ( 2 ( 3 3 ) 2 ) 1 ) 0 CC150里面给出的Code,非常简洁,贴在下面: length == 1 对应…
Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct Node *next;}Node; typedef struct Node *LinkList; LinkedList without head node: LinkedList with head node: Operations: /*check the size of link list.*/i…
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 递归的办法: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { v…
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node. Have you met this question in a real interview?     Example Given 1->2->3->4, and node 3. return 1->2->4 LeetCode上的原题,请参见我之前的博客De…
Reverse a singly linked list. /** * 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 == NUL…
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法   package dsa.linkedlist; public class Node<E>{ E data; Node<E> next; } package dsa.linkedlist; public class ReverseLinkedListRecursively { public static void main(String args[]) { ReverseLinked…
In a singly linked list each node in the list stores the contents of the node and a reference (or pointer in some languages) to the next node in the list. It is one of the simplest way to store a collection of items. In this lesson we cover how to cr…
Reverse a singly linked list  http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html Source   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <stdio.h>   typedef st…
http://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/ 这里的reverse可以reverse整个list,这样空间需求就是O(n),不如这个网页写的O(1)的方法 #include <iostream> #include <vector> #include <algorithm> #include <queue> #include <s…
Jeff Lee blog:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks Linked list is a normal data structure.here I show how to implements it. Step 1. Define a structure public class ListNode { public ListNode Next; publ…
去Twitter面试的被问到这个问题,当时只想到了用HashMap的办法,这种办法时间复杂度O(n),空间复杂度是O(n), 更好的办法是用 FastRunner / SlowRunner approach.用两个pointer遍历链表,fast的速度是slow的两倍,如果有loop,二者一定会collide. boolean detectLoop(LinkedListNode head){ LinkedList slow = head; LinkedList fast = head; whil…
For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void reorderList(ListNode *head…
开始没看懂题目的意思,以为是输入一个单链表,删掉链表中间的那个节点. 实际的意思是,传入的参数就是待删节点,所以只要把当前节点指向下一个节点就可以了. C++ /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */ class Sol…
package LinedList; public class ReverseASinglyLinkedList { //解法一:迭代. public ListNode reverseList(ListNode head) { ListNode previous = null; ListNode current = head; while (current != null) { ListNode next = current.next; current.next = previous; prev…
/* singlyLinkedList.c */ /* 单链表 */ /* 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素. */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> /* 节点结构 */ /* head ———————————————— | value | next | -> ... ———————————————— */ typedef stru…
234. Palindrome Linked List[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? 解法一: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * Lis…
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? 这道题让我们判断一个链表是否为回文链表,LeetCode中关于回文串的题共有六道,除了这道,其他的五道为Palindrome Number 验证回文数字,Validate Palindrome 验证回文字符串,Palindrome Partitioning 拆分回文…
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(n)时间复杂度和O(1)空间复杂度完成吗? 解题思路: 方法一:通过反转链表实现 (1)使用快慢指针寻找链表中点 (2)将链表的后半部分就地逆置 (3)比较前后两半的元素是否一致 (4)恢复原始…
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; * ListNode(int x) : val(x), next(NULL) {} *…
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. * public class ListNode { * int val; * ListNode next;…
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? 分析:题意为判断单链表是否为回文的. 思路:首先想到的是 遍历一次单链表,将其元素装入vector,然后进行第二次遍历比较来判断回文. /** * Definition for singly-linked list. * struct ListNode { * int…
Given a singly linked list, determine if it is a palindrome. 思路: 用快慢指针找到链表中点,反转后半部分链表,然后与前半部分进行匹配,随后将链表恢复原状(本题没有这个要求,具体情况具体对待). C++: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x),…
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra…
题目: 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? 链接: http://leetcode.com/problems/palindrome-linked-list/ 题解: 判断链表是否是Palindrome. 我们分三步解,先用快慢指针找中点,接下来reverse中点及中点后部,最后逐节点对比值. Time…
题目描述: 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(n)的时间复杂度及O(1)的时间复杂度表明顺序遍历链表以及不能够开辟跟链表相当的空间,这样可以找出中间的位置,然后将后半部分的链表反转,然后跟前半部分的链表逐个位置比对. 代码如下: /** * Definition for singl…
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构,将出栈元素与正向移动的指针指向元素比较,即可判断. 解法: /* public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } */ import java.util.Stack; public cla…
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(n) 时间,O(1) 额外空间下完成? 解题思路: 对于数组,判断是否是回文很好办,只需要用两个指针,从两端往中间扫一下就可以判定. 对于单向列表,首先想到的是,将列表复制一份到数组中,然后用上面…
Question 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? Solution 这一题思路并不难.要满足follow-up的要求,我们用到了快慢指针. 1. 用快慢指针得到前后两半list,这里有个技巧是quick先判断有无next,slow再走.这样就保证slow永远指向后半部分的前一个结点 2. Rever…
Total Accepted: 29652 Total Submissions: 117516 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?  (E) Palindrome Number (E) Valid Palindrome (E) Reverse Linked List /…
题目: Given a singly linked list, determine if it is a palindrome. 判断一个单链表是不是回文 思路: 1.遍历整个链表,将链表每个节点的值记录在数组中,再判断数组是不是一个回文数组,时间复杂度为O(n),但空间复杂度也为O(n),不满足空间复杂度要求. 2.利用栈先进后出的性质,将链表前半段压入栈中,再逐个弹出与链表后半段比较.时间复杂度O(n),但仍然需要n/2的栈空间,空间复杂度为O(n). 3.反转链表法,将链表后半段原地翻转,…