2. Add Two Numbers:

/**
* 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* ret, *tmp;
int carry = 0, sum=0;
ret = tmp = new ListNode(0);
while(carry||l1||l2){
sum = carry;
if(l1)sum+=l1->val;
if(l2)sum+=l2->val;
carry = sum/10;
tmp->val = sum%10;
if(l1)l1 = l1->next?l1->next:NULL;
if(l2)l2 = l2->next?l2->next:NULL;
if(!l1 && !l2 && !carry)return ret;
tmp->next = new ListNode(0);
tmp=tmp->next;
}
return NULL;
}
};

19. Remove Nth Node From End of List

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(head == NULL || n == 0) return head;
ListNode* slow = head;
ListNode* fast = head;
ListNode* pre = head;
if(fast->next == NULL) return NULL; //[1] 1
while(n>0){ //if n, slow will just point to the nth
fast = fast->next;
n--;
}
while(fast != NULL){
fast = fast->next;
pre = slow;
slow = slow->next;
}
pre->next = slow->next;
//if pre==slow==head [1,2] 2
if(pre == slow) return head->next; return head;
}
};

  

21. Merge Two Sorted Lists

ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *retList = NULL, *tempList = NULL;
if(!l1 && !l2) return retList;
int val = 0, val1 = 0, val2 = 0;
retList = tempList = new ListNode(0);
while(l1 || l2){
val1 = l1?l1->val:0;
val2 = l2?l2->val:0;
if(((val1<val2) && l1) || !l2){
tempList->val = val1;
if(!(l1->next) && !l2) return retList;
tempList->next = new ListNode(0);
tempList = tempList->next;
l1 = (l1->next)?l1->next:NULL;
continue;
}
else{
tempList->val = val2;
if(!(l2->next) && !l1) return retList; tempList->next = new ListNode(0);
tempList = tempList->next;
l2 = (l2->next)?l2->next:NULL;
continue;
}
}
return retList;
}
//Other guy's solution
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
{
if(!l1) // If no l1, return l2
return l2;
if(!l2) // If no l2, return l1
return l1;
if(!l2 && !l1) // If neither, return NULL;
return NULL; ListNode* head; // The pointer we will use to construct a merged list if(l1->val < l2->val) // If l1 less than l2
{
head = l1; // We start at l1
l1 = l1->next; // and iterate l1
}
else // If l2 less than l1
{
head = l2; // We start at l2
l2 = l2->next; // and iterate l2
} ListNode* ret = head; // We need to save the addres of the head of the list while(l1 && l2) // While both input lists have values
{
if(l1->val < l2->val) // Compare the current values, if l1 is less
{
head->next = l1; // Append the merged list with l1's current address
l1 = l1->next; // Advance l1
}
else // Else, l2 had the low value
{
head->next = l2; // Append l2 to the list
l2 = l2->next; // Advance l2
}
head->next->next = NULL; // Append a NULL teminator to the list
head = head->next; // Advance the merged list
} // Lastly, if list were different lengths, we need to append the longer list tail to the merged list if(l1)
head->next = l1;
else if(l2)
head->next = l2; return ret; // Return the starting address of head that we saved.
}

  

24. Swap Nodes in Pairs

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* first = head;
if(head == NULL || head->next == NULL)return head;
ListNode* second = head->next;
ListNode* ret = second;
ListNode* third = second->next; second->next = first; while(third && third->next){
first->next = third->next;
first = third;
second = third->next;
third = second->next;
second->next = first;
}
if(third == NULL){
second->next = first;
first->next = NULL;
}else{
//third->next =NULL
first->next = third;
}
return ret;
}
};

61. Rotate List

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(head == NULL || k==0) return head;
int len = 1;
ListNode* orighead = head;
ListNode* newhead=head;
while(head->next){
len++;
head = head->next;
}
//loop the link
head->next = orighead;
k = len - k%len-1; //find the front node of kth.
while(k){
newhead = newhead->next;
k--;
}
ListNode* ret = newhead->next;
newhead->next = NULL;
return ret;
}
};

 

83. Remove Duplicates from Sorted List:

/**
* 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) {
ListNode *dup = NULL;
ListNode *cur = head;
if(cur == NULL || cur->next == NULL) return head;
ListNode *nxt = cur->next;
while(nxt != NULL){
if(cur->val == nxt->val){
ListNode *dup = nxt;
nxt = nxt->next;
cur->next = nxt;
delete dup;
}else{
cur = nxt;
nxt = nxt->next;
}
}
return head;
}
};

141. Linked List Cycle

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next; //2step
if(slow == fast) return true;
}
return false;
}
};

   

147. Insertion Sort List

class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode* dummy = new ListNode(0);
ListNode* pre = dummy;
ListNode* curr = head;
if(head == NULL || head->next == NULL) return head;
ListNode* next = NULL; while(curr != NULL){
next = curr->next;
while(pre->next != NULL && pre->next->val < curr->val){ //find insert pos
pre = pre->next;
}
curr->next = pre->next; //insert
pre->next = curr;
curr = next; //move curr to next node
pre = dummy;//reset the pre
}
return dummy->next;
}
};

148. Sort List

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

160. Intersection of Two Linked Lists

/**
* 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* p1 = headA;
ListNode* p2 = headB;
while(p1 != p2){
p1 = p1?p1->next:headB;
p2 = p2?p2->next:headA;
}
return p1;
}
};

203. Remove Linked List Elements  

/**
* 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 == NULL) return head;
ListNode* p = head; while (p->next != NULL){
if(p->next->val == val){
ListNode *tmp = p->next;
p->next = p->next->next;
delete tmp;
}
else{
p = p->next;
}
}
if(head->val == val)
head = head->next;
return head;
}
};

 

206. Reverse Linked List:

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

237. Delete Node in a Linked List  

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
ListNode* p = node->next;
node->val = p->val;
node->next = p->next;
delete p;
}
};

https://www.cnblogs.com/upcwanghaibo/p/6928887.html

https://blog.csdn.net/qq_37466121/article/details/88204678

https://www.cnblogs.com/upcwanghaibo/p/6928887.html

  

  

第4章:LeetCode--链表的更多相关文章

  1. 为什么我要放弃javaScript数据结构与算法(第五章)—— 链表

    这一章你将会学会如何实现和使用链表这种动态的数据结构,这意味着我们可以从中任意添加或移除项,它会按需进行扩张. 本章内容 链表数据结构 向链表添加元素 从链表移除元素 使用 LinkedList 类 ...

  2. [LeetCode] [链表] 相关题目总结

    刷完了LeetCode链表相关的经典题目,总结一下用到的技巧: 技巧 哑节点--哑节点可以将很多特殊case(比如:NULL或者单节点问题)转化为一般case进行统一处理,这样代码实现更加简洁,优雅 ...

  3. Leetcode链表

    Leetcode链表 一.闲聊 边学边刷的--慢慢写慢慢更 二.题目 1.移除链表元素 题干: 思路: 删除链表节点,就多了一个判断等值. 由于是单向链表,所以要删除节点时要找到目标节点的上一个节点, ...

  4. [LeetCode] 链表反转相关题目

    暂时接触到LeetCode上与链表反转相关的题目一共有3道,在这篇博文里面总结一下.首先要讲一下我一开始思考的误区:链表的反转,不是改变节点的位置,而是改变每一个节点next指针的指向. 下面直接看看 ...

  5. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

  6. LeetCode链表相加-Python<二>

    上一篇:LeetCode两数之和-Python<一> 题目:https://leetcode-cn.com/problems/add-two-numbers/description/ 给定 ...

  7. leetcode 链表类型题总结

    链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...

  8. leetcode链表相关

    目录 2/445两数相加 综合题(328奇偶链表, 206反转链表, 21合并两个有序链表 ) 92反转链表 II 链表排序(148排序链表, 876链表的中间结点) 142环形链表 II 160相交 ...

  9. LeetCode 链表题 ( Java )

    leetcode 237. 删除链表中的节点 链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 示例 : 输入: he ...

  10. LeetCode 链表的插入排序

    Sort a linked list using insertion sort 创建一个新的链表,将旧链表的节点插入到正确的位置 package cn.edu.algorithm.huawei; pu ...

随机推荐

  1. 在Ubuntu Server上使用vtk处理体数据,直接得到渲染结果图片避免显示窗口

    概述 需要调用vtk对体数据进行渲染处理,处理结果直接存为图片而不通过窗口显示. 直接使用vtkRenderWindow加上vtkWindowToImageFilter类写入,在调用渲染的过程中会出现 ...

  2. Codeforces Round #597 (Div. 2)

    A - Good ol' Numbers Coloring 题意:有无穷个格子,给定 \(a,b\) ,按以下规则染色: \(0\) 号格子白色:当 \(i\) 为正整数, \(i\) 号格子当 \( ...

  3. 说出Servlet的生命周期,并说出Servlet和CGI的区别。

    说出Servlet的生命周期,并说出Servlet和CGI的区别. 山治ZHrx5 | 浏览 1377 次 推荐于2016-09-16 22:39:19 最佳答案 Servlet的生命周期分为5个阶段 ...

  4. DELPHI正则表达式

    DELPHI正则表达式 1)下载源码 官方网站: http://www.regular-expressions.info/delphi.html     直接下载: http://www.regula ...

  5. IDEA同一项目启动多个实例

    为了验证负载均衡,服务提供者(EurekaClientServiceProviderApplication)需要启动多个实例,当前已启动了一个实例,端口号8762: -- :: --- [ main] ...

  6. Windows7 安装docker工具的方法

    1.参考官方文档 https://docs.docker.com/toolbox/toolbox_install_windows/ 注意:因为我的电脑是windows10 家庭版,所以,无法使用 Do ...

  7. LeetCode_169. Majority Element

    169. Majority Element Easy Given an array of size n, find the majority element. The majority element ...

  8. WIN 10 看不到SAMBA共享的硬盘

    1.SMB1.0/CIFS协议默认被关闭了,之前的勒索病毒就是用的这个协议的漏洞,所以你去“启动和关闭windows功能”下手动勾选启用SMB1.0/CIFS协议 2.管理员身份执行 sc.exe c ...

  9. 细说SQL Server中的加密

    简介 加密是指通过使用密钥或密码对数据进行模糊处理的过程.在SQL Server中,加密并不能替代其他的安全设置,比如防止未被授权的人访问数据库或是数据库实例所在的Windows系统,甚至是数据库所在 ...

  10. react中,用key值来解决一些奇葩问题

    编辑用户信息,角色信息无法加载到值 改进之后:思路:由于值是设置在state里面的,界面编辑时,会重服务器拉去数据,值也设置在state里面了,但是CheckboxGroup依然不会去渲染选中的值, ...