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. Go开发环境安装与环境变量配置

    1.Go安装包下载 https://studygolang.com/dl 2.安装 Windows平台下,直接下一步即可完成安装. 3.配置环境变量 系统变量 安装完成后,在系统变量的Path一栏,会 ...

  2. elasticsearch配置jdk

    编辑bin/elasticsearch 可以看到elasticsearch使用环境变量JAVA_HOME中配置的jdk:if [ -x "$JAVA_HOME/bin/java" ...

  3. CodeForces - 1175D Array Splitting(数组划分+后缀和+贪心)

    You are given an array a1,a2,…,ana1,a2,…,an and an integer kk. You are asked to divide this array in ...

  4. 解决用root用户及密码可以直接登陆某LINUX系统,但是用ssh登陆,系统却总是提示密码不对

    引用 vi /etc/ssh/sshd_config 将PermitRootLogin项改为yes service sshd restart 重启sshd服务即可

  5. 项目中一次排序规则的改动,注意到js中map的遍历的顺序

    背景:项目需要对前端页面上某个插件的下拉选择项进行排序,需要按照配置的顺序显示. 首先调查后台,发现sql语句中已经添加order by.之后发现查询结果遍历后封装进HashMap,这里改为LinkH ...

  6. Dataeye计算任务架构

    https://mp.weixin.qq.com/s/9Q5-oU3bPIBieScwzrawDg 资源消耗降低2/3,Flink在唯品会实时平台的应用(有彩蛋) 王新春 DBAplus社群 2018 ...

  7. iptables+ipset自动封闭和解封频繁访问web服务的恶意IP

    转载于互联网     iptables直接针对ip进行封禁,在ip数量不大的时候是没什么问题的,但当有大量ip的时候性能会严重下降,iptables是O(N)的性能.而ipset就像一个集合,把需要封 ...

  8. iOS 当键盘覆盖textFiled时简单的处理方法

    //方法1--- - (void)textFieldDidBeginEditing:(UITextField *)textField { if (iPhone5) { return; } else { ...

  9. nginx多个if条件并且查询

    set $flag 0; if (!-e $request_filename){    set $flag "${flag}1"; } if ($uri ~* "^(.* ...

  10. robot:生成随机的8为纯数字

    1.引进random库 2.注意最后面的random为需要引入的包