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. 6.Python3字符串和格式化

    一.字符串 1.字符串表示方法 2.字符串的序号 3.字符串的使用 4.字符串切片 5.字符串的特殊字符 6.字符串操作符 案例:输入对应的数字显示对应的星期 '''weekStr = "星 ...

  2. c++ 珊格迷宫问题

    #demo1 #include<iostream> #include<ctime> #include<cstdlib> #include<queue> ...

  3. (转)外网如何访问docker容器

    借鉴:https://blog.csdn.net/lvshaorong/article/details/69950694 Docker容器非常轻量,系统开销非常少,比VMware或者VirtualBo ...

  4. [端口安全]Hydra密码爆破

    目录 0x01 简介 0x02 常见参数 0x03 使用案例 0x04 密码字典 0x01 简介 Hydra中文名:九头蛇,这是一款相当强大的爆破工具,它基本支持了所有可爆破协议,而且容容错率非常好 ...

  5. Leetcode题目437:路径总和III(递归-简单)

    题目描述: 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二 ...

  6. java集合类型源码解析之PriorityQueue

    本来第二篇想解析一下LinkedList,不过扫了一下源码后,觉得LinkedList的实现比较简单,没有什么意思,于是移步PriorityQueue. PriorityQueue通过数组实现了一个堆 ...

  7. ngx.shared.DICT.get 详解

    ngx.shared.DICT.get 原文: ngx.shared.DICT.get syntax: value, flags = ngx.shared.DICT:get(key) context: ...

  8. CTR预估之LR与GBDT融合

    转载自:http://www.cbdio.com/BigData/2015-08/27/content_3750170.htm 1.背景 CTR预估,广告点击率(Click-Through Rate ...

  9. Git的概念和基本使用

    概念篇 1. Git简介: 鉴于有些同学可能还不知道Git是什么,我首先对Git做个简短的介绍.Git就是类似于svn的一个版本控制工具,他其实和hg更像一些,hg也是一个分布式版本控制工具,可以说g ...

  10. 微信小程序之圆形进度条(自定义组件)

    思路 使用2个canvas 一个是背景圆环,一个是彩色圆环. 使用setInterval 让彩色圆环逐步绘制. 在看我的文章前,必须先看 ,下面转的文章,因为本文是在它们基础上修改的. 它们的缺点为: ...