141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer
pos
which represents the position (0-indexed) in the linked list where tail connects to. Ifpos
is-1
, then there is no cycle in the linked list.Example 1:
Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0 Output: true Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1 Output: false Explanation: There is no cycle in the linked list.
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
//Solution
//总结:如果存在环的话,单步和双步迟早会遇到,可以以三元环为例子
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
if(!head || !head->next) return false;//必须先判断
struct ListNode *sig=head, *doub=head->next; //一开始再放前面,如果head为空就会错误
while(doub && doub->next)
{
sig = sig->next;//单步
doub =doub->next->next;//双步
if(sig==doub) return true;//存在环
}
return false;
}
155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) – Push element x onto stack.
- pop() – Removes the element on top of the stack.
- top() – Get the top element.
- getMin() – Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> Returns -3. minStack.pop(); minStack.top(); --> Returns 0. minStack.getMin(); --> Returns -2.
//Solution
//总结:
class MinStack {
public:
/** initialize your data structure here. */
stack<int> data, minimum;
MinStack() {
}
void push(int x) {
data.push(x);
if (minimum.empty() || minimum.top() >= x) {
minimum.push(x);
}
}
void pop() {
if (top() == getMin()) {
minimum.pop();
}
data.pop();
}
int top() {
return data.top();
}
int getMin() {
return minimum.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
160. 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 → b3Notes:
- If the two linked lists have no intersection at all, return
null
.- The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory.
//Solution
//总结:
/*
共同的node: Common
listA = A + Common
listB = B + Common
PointerA: [A + Common + B] + Common
PointerB: [B + Common + A] + Common
所以当两边第一个的 common 走完(走到最后一个node),就可以两两互相比较,一定一起走到下一个 Common,因为 [A + Common + B] = [B + Common + A]
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode *p1 = headA;
struct ListNode *p2 = headB;
if (NULL == p1 || NULL == p2)
return NULL;
while (p1 != NULL && p2 != NULL && p1 != p2)
{
p1 = p1->next;
p2 = p2->next;
if(p1 == p2)
return p1;
if(NULL == p1)
p1 = headB;
if(NULL == p2)
p2 = headA;
}
return p1;
}