几道复习链表的基础题
Posted programchen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了几道复习链表的基础题相关的知识,希望对你有一定的参考价值。
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
1 /*
2 struct ListNode {
3 int val;
4 struct ListNode *next;
5 ListNode(int x) :
6 val(x), next(NULL) {
7 }
8 };
9 */
10 class Solution {
11 public:
12 ListNode* deleteDuplication(ListNode* pHead)
13 {
14 if(pHead == NULL || pHead->next == NULL)
15 {
16 return pHead;
17 }
18 ListNode *newhead = new ListNode(-1);
19 newhead->next = pHead;
20 ListNode *pre = newhead;
21 ListNode *cur = pHead;
22 ListNode *next = NULL;
23 while(cur != NULL && cur->next != NULL)
24 {
25 next = cur->next;
26 if(cur->val == next->val)
27 {
28 while(next != NULL && next->val == cur->val)
29 {
30 next = next->next;
31
32 }
33 pre->next = next;
34 cur = next;
35 }
36 else
37 {
38 pre = cur;
39 cur = cur->next;
40 }
41 }
42
43 return newhead->next;
44
45 }
46 };
编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据顺序不变。
1 /*
2 struct ListNode {
3 int val;
4 struct ListNode *next;
5 ListNode(int x) : val(x), next(NULL) {}
6 };*/
7 class Partition {
8 public:
9 ListNode* partition(ListNode* pHead, int x) {
10 // write code here
11 ListNode *small = new ListNode(0);
12 ListNode *big = new ListNode(0);
13 ListNode *smallhead = small;
14 ListNode *bighead = big;
15 while(pHead!= NULL)
16 {
17 if(pHead->val < x)
18 {
19 small->next = pHead;
20 small = small->next;
21 }
22 else
23 {
24 big->next = pHead;
25 big = big->next;
26 }
27 pHead = pHead->next;
28 }
29 big->next = NULL;
30 small->next = bighead->next;
31 return smallhead->next;
32
33 }
34 };
输入一个链表,输出该链表中倒数第k个结点。
1 /*
2 struct ListNode {
3 int val;
4 struct ListNode *next;
5 ListNode(int x) :
6 val(x), next(NULL) {
7 }
8 };*/
9 class Solution {
10 public:
11 ListNode* FindKthToTail(ListNode* pListHead, unsigned int k)
12 {
13 if(pListHead==NULL|| k<=0)
14 {
15 return NULL;
16 }
17 vector<ListNode*> v;
18 ListNode* head = pListHead;
19 while(head)
20 {
21 v.push_back(head);
22 head = head->next;
23 }
24 if(v.size()>=k)
25 {
26 return v[v.size()-k];
27 }
28 else return NULL;
29
30 }
31 };
1 /*
2 struct ListNode {
3 int val;
4 struct ListNode *next;
5 ListNode(int x) :
6 val(x), next(NULL) {
7 }
8 };*/
9 class Solution {
10 public:
11 ListNode* FindKthToTail(ListNode* pListHead, unsigned int k)
12 {
13 if(pListHead==NULL|| k<=0)
14 {
15 return NULL;
16 }
17 stack<ListNode*> str;
18 ListNode* tmp=pListHead;
19 while(tmp!=NULL)
20 {
21 str.push(tmp);
22 tmp=tmp->next;
23 }
24 if(str.size()>=k)
25 {
26 while((--k)>0)
27 {
28 str.pop();
29 }
30 return str.top();
31 }
32 return NULL;
33
34 }
35 };
反转链表
1 /**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode(int x) : val(x), next(NULL) {}
7 * };
8 */
9 class Solution {
10 public:
11 ListNode* reverseList(ListNode* head) {
12 ListNode *prev,*cur;
13 prev = NULL;
14 cur=head;
15 while(cur!=NULL)
16 {
17 ListNode *tmp=cur->next;
18 cur->next=prev;
19 prev=cur;
20 cur=tmp;
21 }
22 return prev;
23 }
24 };
链表的中间节点
1 /**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode(int x) : val(x), next(NULL) {}
7 * };
8 */
9 class Solution {
10 public:
11 ListNode* middleNode(ListNode* head) {
12 vector<ListNode*> v;
13 ListNode *tmp=head;
14 while(tmp)
15 {
16 v.push_back(tmp);
17 tmp=tmp->next;
18 }
19 return v[v.size()/2];
20 }
21 };
合并两个有序链表
1 /**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode(int x) : val(x), next(NULL) {}
7 * };
8 */
9 class Solution {
10 public:
11 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
12
13
14 ListNode* sum =new ListNode(1);
15 ListNode* ptr = sum;
16 while(l1!=NULL && l2!=NULL)
17 {
18 if(l1->val >= l2->val)
19 {
20 ptr->next = l2;
21 ptr=l2;
22 l2=l2->next;
23 }
24 else
25 {
26 ptr->next = l1;
27 ptr = l1;
28 l1 = l1->next;
29
30 }
31
32 }
33 ptr->next = l1==NULL ? l2 : l1;
34 sum=sum->next;
35 return sum;
36 }
37 };
1 /**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode(int x) : val(x), next(NULL) {}
7 * };
8 */
9 class Solution {
10 public:
11 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
12 vector<int> v;
13 ListNode* l1tmp = l1;
14 ListNode* l2tmp = l2;
15 while(l1tmp != NULL)
16 {
17 v.push_back(l1tmp->val);
18 l1tmp=l1tmp->next;
19 }
20 while(l2tmp != NULL)
21 {
22 v.push_back(l2tmp->val);
23 l2tmp=l2tmp->next;
24 }
25 sort(v.begin(),v.end(),less<int>());
26 ListNode* l3 = new ListNode(0);
27 ListNode* l3tmp = l3;
28 for(auto &i : v)
29 {
30 ListNode* tmp = new ListNode(i);
31 l3tmp->next = tmp;
32 l3tmp = l3tmp->next;
33 }
34 return l3->next;
35
36 }
37 };
以上是关于几道复习链表的基础题的主要内容,如果未能解决你的问题,请参考以下文章
NC41 最长无重复子数组/NC133链表的奇偶重排/NC116把数字翻译成字符串/NC135 股票交易的最大收益/NC126换钱的最少货币数/NC45实现二叉树先序,中序和后序遍历(递归)(代码片段