92-按规定区间反转链表

思路:可以考虑成一种把前后数字的结点断开重新组合的问题

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode *dummy = new ListNode(-1), *pre = dummy;
dummy->next = head;
for (int i = 0; i < m - 1; ++i)
pre = pre->next;
ListNode *cur = pre->next;
for (int i = m; i < n; ++i) {
ListNode *t = cur->next;
cur->next = t->next;
t->next = pre->next;
pre->next = t;
}
return dummy->next;
}
};

86-分割链表

思路:先找到一个大于或者等于给定值的节点,然后再逐个把小于他们的值放在前面。例如本例先找到4,然后再找到3,然后把小于3的值都放在其前面

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode *p=new ListNode(-1);
p->next=head;
ListNode *pre=p,*cur=head;
while(pre->next&&pre->next->val<x)
pre=pre->next;
cur=pre;
while(cur->next){
if(cur->next->val<x){
ListNode *tmp=cur->next;
cur->next=tmp->next;
tmp->next=pre->next;
pre->next=tmp;
pre=pre->next;
}else{
cur=cur->next;
}
}
return p->next;
}
};

2-逆序链表存储数相加

思路:先建立一个p结点,然后将相加生成的新结点按顺序放到p结点之后,然后再用一个新指针cur指向新链表的最后一位。设置一个进位计数res,当两个结点值相加之后,可以用sum/10来表示进位,然后以sum%10来建立新的结点。最后需要注意的是最高位的进位问题,所以while结束后要,如果res为1,则再建一个值为1的结点。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *p=new ListNode(-1),*cur=p;
int res=0;
while(l1||l2){
int val1=l1?l1->val:0;
int val2=l2?l2->val:0;
int sum=val1+val2+res;
res=sum/10;
cur->next=new ListNode(sum%10);
cur=cur->next;
if(l1)
l1=l1->next;
if(l2)
l2=l2->next;
}
if(res)
cur->next=new ListNode(1);
return p->next;
}
};

445-顺序链表存储相加

思路:这道题和第2题类似,但是链表是从前往后遍历,加法却要从最低位相加,所以可以考虑改用栈来存储放进来的数据。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> s1, s2;
while (l1) {
s1.push(l1->val);
l1 = l1->next;
}
while (l2) {
s2.push(l2->val);
l2 = l2->next;
}
int sum = 0;
ListNode *res = new ListNode(0);
while (!s1.empty() || !s2.empty()) {
if (!s1.empty()) {
sum += s1.top();
s1.pop();
}
if (!s2.empty()) {
sum += s2.top();
s2.pop();
}
res->val = sum % 10;
ListNode *cur = new ListNode(sum / 10);
cur->next = res;
res = cur;
sum /= 10;
}
return res->val == 0 ? res->next : res;
}
};

203-移除链表元素

思路:直接递归调用到链表末尾,然后回来,需要删除的元素将链表next指针指向下一个元素即好。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(!head) return NULL;
head->next=removeElements(head->next,val);
return head->val==val?head->next:head;
}
};

82-删除排序链表中的重复元素

思路:递归查找,如果head的值存在且相等,那么while循环跳过后面所有值相等的结点,如果后面if还有值相等则继续进行递归。如果最后到head的值不同后,返回到head即可。<这种方式比新建链表存储时间负责度高很多>

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head) return head;
if (head->next && head->val == head->next->val) {
while (head->next && head->val == head->next->val) {
head = head->next;
}
return deleteDuplicates(head->next);
}
head->next = deleteDuplicates(head->next);
return head;
}
};

83-删除顺序链表中的重复元素

思路:head结点的值和身后结点的值进行比较,如果值相同,则返回后面一个结点。最后回溯递归调用删除重复结点。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head||!head->next) return head;
head->next=deleteDuplicates(head->next);
return (head->val==head->next->val)?head->next:head;
}
};

  

最新文章

  1. NPTL vs PThread
  2. WEB UI 整理
  3. laravel框架中widget模糊查询
  4. Go prepare statment超过mysql最大数
  5. seaJS常用语法
  6. wpf 仿QQ音乐歌词卡拉OK
  7. FS,FT,DFS,DTFT,DFT,FFT的联系和区别
  8. [Windows Azure] Querying Tables and Entities
  9. 【HDOJ】2206 IP的计算
  10. HTML&amp;CSS基础学习笔记1.11—导航栏
  11. 该如何关闭thinkphp的缓存呢?有下面几种方法可参考:
  12. Stones(优先队列)
  13. node中的get请求和post请求的不同操作【node学习第五篇】
  14. Hibernate在自由状态和持久的状态转变
  15. [Selenium With C#学习笔记] Lesson-02 Web元素定位
  16. 解决vsftp无法启动问题(转)
  17. hive分组排序 取top N
  18. 精选20道Java代码笔试题
  19. 【JEECG技术文档】JEECG高级查询构造器使用说明
  20. Spring系列之——springboot解析resources.application.properties文件

热门文章

  1. [CSP-S模拟测试]:平均数(二分答案+归并排序)
  2. python- &#39; % &#39;运算符的用途(非常重要)
  3. CSL 的密码(后缀数组)
  4. dubbo远程服务调用和maven依赖的区别
  5. css怎么解决表格边框线重复问题
  6. 金额格式化,例子:fmoney(&quot;12345.675910&quot;, 3),返回12,345.676
  7. 目录---Asp.NETCore轻松学系列【目录】
  8. LAN VLAN与VXLAN学习笔记
  9. 七、OIDC
  10. 转:动态库路径配置- /etc/ld.so.conf文件