摘要:
此题有点意思,先用快慢指针把链表从一半处断开,然后把后半段倒过来,然后再进行merge。注意断开时要把前面链表的最后置NULL,merge后要把最后节点next置NULL。#include #include #include using namespace std;class Solution {public: ListNode * reverseList(ListNode *head) { if (head == NULL) return NULL; ListNode *dummy = new ListNode(0); dummy->next ... 阅读全文
posted @ 2013-12-03 22:20
阿牧遥
阅读(186)
评论(0)
推荐(0)
摘要:
http://oj.leetcode.com/problems/linked-list-cycle-ii/老题。当快慢指针相交时,通过方程或观察可知,从head到环开始点的距离和从相遇点开始是一样的,那么从相遇点和从开始点再一起走直到相遇就行了。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ... 阅读全文
posted @ 2013-12-03 21:32
阿牧遥
阅读(169)
评论(0)
推荐(0)
摘要:
http://oj.leetcode.com/problems/linked-list-cycle/老题,快慢指针。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head) { ListNode *fast = head; ... 阅读全文
posted @ 2013-12-03 21:21
阿牧遥
阅读(132)
评论(0)
推荐(0)

浙公网安备 33010602011771号