1题目描述
输入一个链表,输出该链表中倒数第k个结点。
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { ListNode*head = pListHead; ListNode*nodeK=pListHead; int len=0;//链表总长 int index=0;// if(k<=0){ return NULL; } while(head!=NULL){ len++; head=head->next; } if(k>len){ return NULL; } index = len-k; while(index--){ nodeK = nodeK->next; } return nodeK; } };
View Code
2题目描述
实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。
给定带删除的节点,请执行删除操作,若该节点为尾节点,返回false,否则返回true
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/ class Remove { public: bool removeNode(ListNode* pNode) { // write code here if(pNode->next == NULL){ delete pNode; return false; }else{ ListNode* nextNode; nextNode = pNode->next; pNode->val = nextNode->val; pNode->next = nextNode->next; delete nextNode; return true; } } };
View Code
3题目描述
编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据顺序不变。
class Partition { public: ListNode* partition(ListNode* pHead, int x) { // write code here ListNode* small=new ListNode(0); ListNode* large=new ListNode(0); ListNode* smallHead=small; ListNode* largeHead=large; while(pHead){ if(pHead->val<x){ small->next=pHead; small=small->next; } else{ large->next=pHead; large=large->next; } pHead=pHead->next; } large->next=NULL; small->next=largeHead->next; return smallHead->next; } };
View Code
4题目描述
有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。
给定两个链表ListNode* A,ListNode* B,请返回A+B的结果(ListNode*)。
测试样例:
{1,2,3},{3,2,1}
返回:{4,4,4}
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/ class Plus { int getNum(ListNode* a){ int num=0; while(a){ num += a->val; a=a->next; num=num*10; } return num/10; } ListNode * reverseNode(ListNode* a){ ListNode* head = a; ListNode* pre ,*front,*p; if (a == NULL){ return NULL; } front = a->next; pre = a; while (front){ p = front->next; front->next = pre; pre = front; front = p; } head->next = NULL; return pre; } public: ListNode* plusAB(ListNode* a, ListNode* b) { // write code here int numA = getNum(reverseNode(a)); int numB = getNum(reverseNode(b)); int numC = numA+numB; int tmp; ListNode *head = new ListNode(-1); ListNode *p = head; while(numC){ tmp = numC%10; numC /=10; p->next = new ListNode(tmp); p = p->next; } return head->next; } };
View Code
5题目描述
请编写一个函数,检查链表是否为回文。
给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。
测试样例:
{1,2,3,2,1}
返回:true
{1,2,3,2,3}
返回:false
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/ class Palindrome { public: bool isPalindrome(ListNode* pHead) { vector<int>a; ListNode* tmp = pHead; while(tmp){ a.push_back(tmp->val); tmp = tmp->next; } int len = a.size(); for(int i=0;i<len/2; i++){ if(a[i] != a[len-i-1]) return false; } return true; // write code here } };
View Code
原文链接: https://www.cnblogs.com/yuguangyuan/p/6124401.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/397855
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!