1.移除值位val的元素
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(head == NULL)
return NULL;
while(head != NULL && val == head->val )
head = head->next;
ListNode* pCur = head;
while(pCur && pCur->next)
{
if(val == pCur->next->val)
pCur->next = pCur->next->next;
else
pCur = pCur->next;
}
return head;
}
};
2.链表的逆置
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head){
if(head == NULL)
return NULL;
ListNode* PreNode = NULL;
ListNode* CurNode = head;
ListNode* NexNode = NULL;
while(1)
{
NexNode = CurNode->next;
CurNode->next = pre;
pre = CurNode;
CurNode = NexNode;
}
return PreNode;
}
3.返回中间链表
class Solution {
public:
ListNode* middleNode(ListNode* head) {
if(head == NULL || head->next == NULL)
return head;
ListNode* slow = head;
ListNode* fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};
4.返回倒数第K个节点
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if(pListHead == NULL || k <= 0)
return NULL;
ListNode* Fast = pListHead;
ListNode* Slow = pListHead;
while(k--)
{
if(Fast == NULL)
return NULL;
Fast = Fast->next;
}
while(Fast)
{
Fast = Fast->next;
Slow = Slow->next;
}
return Slow;
}
};
5.将两个有序链表合并成一个有序链表
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
if(l1->val < l2->val)
{
l1->next = mergeTwoLists(l1->next,l2);
return l1;
}
else
{
l2->next = mergeTwoLists(l1,l2->next);
return l2;
}
}
};
6.以x为基准分开
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
// write code here
if(pHead == NULL)
return NULL;
ListNode* BeforeHead = NULL;
ListNode* BeforeNode = NULL;
ListNode* AfterHead = NULL;
ListNode* AfterNode = NULL;
ListNode* CurNode = pHead;
ListNode* CurNext = pHead;
while(CurNode)
{
CurNext = CurNode->next;
if(CurNode->val < x)
{
if(BeforeHead == NULL)
{
BeforeHead = CurNode;
BeforeNode = BeforeHead;
BeforeNode->next = NULL;
CurNode = CurNext;
continue;
}
BeforeNode->next = CurNode;
BeforeNode = BeforeNode->next;
BeforeNode->next = NULL;
CurNode = CurNext;
}
else
{
if(AfterHead == NULL)
{
AfterHead = CurNode;
AfterNode = AfterHead;
AfterNode->next = NULL;
CurNode = CurNext;
continue;
}
AfterNode->next = CurNode;
AfterNode = AfterNode->next;
AfterNode->next = NULL;
CurNode = CurNext;
}
}
if(BeforeHead == NULL)
return AfterHead;
BeforeNode->next = AfterHead;
return BeforeHead;
}
};
7.删除重复节点
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead == NULL) {
return NULL;
}
ListNode *preNode = NULL;
ListNode *node = pHead;
while (node != NULL)
{
if (node->next != NULL && node->val == node->next->val)
{
int value = node->val;
while (node->next != NULL && node->next->val == value)
{
node = node->next;
}
if (preNode == NULL)
{
pHead = node->next;
}
else
{
preNode->next = node->next;
}
}
else
{
preNode = node;
}
node = node->next;
}
return pHead;
}
};
8.回文链表
class PalindromeList {
public:
bool chkPalindrome(ListNode* A) {
// write code here
if(A == NULL || A->next == NULL)
return false;
ListNode* Cur = A;
int flag = Cur->val;
Cur = Cur->next;
while(Cur)
{
flag = flag ^ Cur->val;
Cur = Cur->next;
}
if(flag == 0) return true;
else return false;
}
};
class PalindromeList {
public:
bool chkPalindrome(ListNode* A) {
// write code here
if(A == NULL)
return A;
ListNode* Min = A;
ListNode* Cur = A;
ListNode* MinHead = NULL;
while(Cur!=NULL && Cur->next!=NULL)
{
Cur = Cur->next->next;
Min = Min->next;
}
Cur = A;
MinHead = Min->next;
Min->next = NULL;
ListNode* Pre = NULL;
ListNode* Nex = NULL;
while(MinHead)
{
Nex = MinHead->next;
MinHead->next = Pre;
Pre = MinHead;
MinHead = Nex;
}
while(Pre)
{
if(Pre->val!=Cur->val)
return false;
Pre = Pre->next;
Cur = Cur->next;
}
return true;
}
};
9.相交链表相交点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *pTail1 = headA;
ListNode *pTail2 = headB;
while(pTail1)
pTail1=pTail1->next;
while(pTail2)
pTail2=pTail2->next;
if(pTail1 != pTail2)
return NULL;
ListNode *pCur1 = headA;
ListNode *pCur2 = headB;
int size1 = GetLength(pCur1);
int size2 = GetLength(pCur2);
int gap = size1 - size2;
if(gap > 0 )
{
while(gap--)
pCur1=pCur1->next;
}
else
{
while(gap !=0)
{
pCur2=pCur2->next;
gap++;
}
}
while(pCur1 != pCur2)
{
pCur1=pCur1->next;
pCur2=pCur2->next;
}
return pCur1;
}
int GetLength(ListNode *head)
{
int count = 0;
ListNode *pCur = head;
while(pCur)
{
count++;
pCur= pCur->next;
}
return count;
}
};
10 链表是否带环
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == NULL || head->next == NULL)
return false;
ListNode* Fast = head;
ListNode* slow = head;
while(Fast!=NULL && Fast->next!= NULL && slow != NULL)
{
Fast = Fast->next->next;
slow = slow->next;
if(slow == Fast)
return true;
}
return false;
}
};
11.带环链表的入口
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL || head->next == NULL)
return NULL;
ListNode* Fast = head;
ListNode* Slow = head;
while(Fast != NULL && Fast->next != NULL)
{
Fast = Fast->next->next;
Slow = Slow->next;
if(Fast == Slow)
break;
}
if(Fast != Slow)
return NULL;
Slow = head;
while(Fast != Slow)
{
Slow = Slow->next;
Fast = Fast->next;
}
return Slow;
}
};
12.复杂链表的复制
class Solution {
public:
Node* copyRandomList(Node* head) {
if(head == nullptr)
return head;
Node *node = head;
while(node!=nullptr)
{
Node* copy = new Node(node->val,nullptr,nullptr);
copy->next = node->next;
node->next = copy;
node= copy->next;
}
node = head;
while(node != nullptr)
{
if(node->random != nullptr)
node->next->random = node->random->next;
node = node->next->next;
}
node = head;
Node* pNewhead = head->next;
Node* pNewNode = pNewhead;
while(node!=nullptr)
{
node->next = node->next->next;
if(pNewNode->next !=nullptr)
pNewNode->next = pNewNode->next->next;
node = node->next;
pNewNode = pNewNode->next;
}
return pNewhead;
}
};
输出二叉树第K大节点
TreeNode* KthNode(TreeNode* root, int k){
if(!root) return NULL;
TreeNode *l=KthNode(root->left,k);
if(cnt>=k) return l;
cnt++;
if(cnt==k) return root;
return KthNode(root->right,k);
}
寻找二叉树公共节点
O(N^2)
BinaryNode * GetLastCommonAncestor(BinaryNode * root, BinaryNode * node1, BinaryNode * node2)
{
BinaryNode * temp;
while (node1 != NULL)
{
node1 = node1->_parent;c
temp = node2;
while (temp != NULL)
{
if (node1 == temp->_parent)
return node1;
temp = temp->_parent;
}
}
}
O(n)
int Hight(BinaryNode* root, BinaryNode* node)
{
int len = 0;
for (; node != NULL; node = node->_parent)
len++;
return len;
}
BinaryNode* GetLastCommonAncestor(BinaryNode* root, BinaryNode* node1, BinaryNode* node2)
{
if (root == NULL || node1 == NULL || node2==NULL)
return NULL;
int len1 = Hight(root,node1);
int len2 = Hight(root,node2);
for (; len1 > len2; len1--)
node1 = node1->_parent;
for (; len2 > len1; len2--)
node2 = node2->_parent;
while (node1 && node2 && node1 != node2)
{
node1 = node1->_parent;
node2 = node2->_parent;
}
if (node1 == node2)
return node1;
else
return NULL;
}
递归
BinaryNode* GetLastCommonAncestor(BinaryNode* root, BinaryNode* node1, BinaryNode* node2)
{
if (root == NULL || node1 == NULL || node2 == NULL)
return NULL;
if (node1 == root || node2 == root)
return root;
BinaryNode* cur = NULL;
BinaryNode* left_lca = GetLastCommonAncestor(root->_left, node1, node2);
BinaryNode* right_lca = GetLastCommonAncestor(root->_right, node1, node2);
if (left_lca && right_lca)
return root;
if (left_lca == NULL)
return right_lca;
else
return left_lca;