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. yarn 强制孙依赖的版本

    今天博主遇到一个棘手的问题,@vue/cli-service 依赖了一个包 portfiner@^1.0.20,但是 2 天前,这个包更新到了1.0.22,带来了一些问题. 博主第一反应就是想 yar ...

  2. 配置interfaces

    demo1 # This file describes the network interfaces available on your system # and how to activate th ...

  3. 如何十倍提高你的webpack构建效率

    前言 http://jafeney.com/2016/07/10/2016-07-10-webpack/     webpack 是个好东西,和 NPM 搭配起来使用管理模块实在非常方便.而 Babe ...

  4. mp4文件格式解析(转)

    mp4文件格式解析 MP4文件格式带数据详解 MP4文件格式的解析,以及MP4文件的分割算法

  5. LC 375. Guess Number Higher or Lower II

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  6. Leetcode: Find First and Last Position of Element in Sorted Array

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  7. ASP程序中调用Now()总显示“上午”和“下午”,如何解决?

    ASP程序中调用Now()总显示这样的格式:“2007-4-20 下午 06:06:38”,我要的正确格式为“2007-4-20 18:06:38”,我已经通过控制面板==>区域和语言选项==& ...

  8. C#依赖注入实例

    http://qing.weibo.com/tj/400082fa33001h7x.html 1.5 实现依赖注入1.5.1 背景介绍 设计模式中,尤其是结构型模式很多时候解决的就是对象间的依赖关系, ...

  9. 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_03-用户认证-认证服务查询数据库-查询用户接口-接口定义

    1.2.4 查询用户接口 完成用户中心根据账号查询用户信息接口功能. 在ucenter这个服务里面定义查询用户信息的接口 这个接口在auth的服务的loadUserByUserName这个方法里面被调 ...

  10. 算法习题---3.12浮点数(UVa11809)

    一:题目 尴尬的非会员水印 二:题目摘要 1.int和float比较 int共32位,可以表示的最大的数为2^32次方 float虽然也是32位,但是是以指数形式保存,指数占8位(含符号),最大127 ...