二、链表问题
司曹龙学编程
多学习 多努力 好好学编程
展开
-
leetcode206 链表的反转
Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or recursi...原创 2018-08-06 14:07:58 · 104 阅读 · 0 评论 -
143. Reorder List
题目 就是把一个链表的首节点与尾结点链接 次首位与次尾链接例如:1-->2-->3--->4 输出:1--4-->2-->3;思路 :将这个链表分为两部分 left 和right;(涉及到找到链表的中点的问题) 然后对右侧的进行一个reverse操作; 然后再分别从两个中取出元素;进行重新赋值到原来的链表中; 代码: /** ...原创 2018-08-07 10:12:54 · 94 阅读 · 0 评论 -
leetcode 148 重点中的重点 链表的排序
思路:时间复杂度 olongn 必须用 归并排序;归并排序的思想 就是分治的思想将整个链表分为两部分 ,假设这两部分是排序完成的就 直接 merge他们两个;需要几个函数 :get_mid找到链表的中间节点; merge()将两个有序的链表合并为一个链表CODE/** * Definition for singly-linked list. * struct Lis...原创 2018-08-07 10:12:47 · 192 阅读 · 0 评论 -
leetcode 147 Insertion Sort List
思路 :首先要知道插入排序的思想 就是从前往后遍历 当遇到一个数比前面的数小的时候 就把这个数 拿出来 把它插入它应该在的位置;所以 就要 当前结点的前一个结点的下一个结点 指向这个结点的下一个结点也就是pre-->cur--->pNext;pre->next=pNext;把cur这个结点拿出来 将它插入它应该在的位置; 需要从前遍历 如果结点比这个结点...原创 2018-08-07 10:12:36 · 136 阅读 · 0 评论 -
leetcode 24. Swap Nodes in Pairs 每两个结点反转一次
Given a linked list, swap every two adjacent nodes and return its head.Example:Given 1->2->3->4, you should return the list as 2->1->4->3.Note:Your algorithm should use only...原创 2018-08-07 10:12:29 · 149 阅读 · 0 评论 -
leetcode 82 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.Example 1:Input: 1->2->3->3->4->4->5Output: 1->...原创 2018-08-07 10:12:23 · 107 阅读 · 0 评论 -
leetcode 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 {p...原创 2018-08-07 10:12:17 · 85 阅读 · 0 评论 -
445. 两数相加 II
给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。进阶:如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。示例:输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)输出...原创 2018-08-07 10:12:03 · 574 阅读 · 0 评论 -
leetcode 2 链表中的两数之和;
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。示例:输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807/** * Definition...原创 2018-08-07 10:11:50 · 337 阅读 · 0 评论 -
leetcode 328 328. Odd Even Linked List 将链表的奇数偶数位置分开
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in p...原创 2018-08-06 20:57:49 · 424 阅读 · 0 评论 -
61. Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplan...原创 2018-08-06 20:35:33 · 110 阅读 · 0 评论 -
25. k个一组翻转链表
给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表。k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序。示例 :给定这个链表:1->2->3->4->5当 k = 2 时,应当返回: 2->1->4->3->5当 k = 3 时,应当返回: 3->2->...原创 2018-08-06 17:59:28 · 117 阅读 · 0 评论 -
leetcode 21 合并两个有序链表
直接上代码 就不解释了 意义不大;/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {pub...原创 2018-08-06 17:17:01 · 93 阅读 · 0 评论 -
leetcode 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Example:Input:[ 1->4->5, 1->3->4, 2->6]Output: 1->1->2->3->4-...原创 2018-08-06 17:13:15 · 158 阅读 · 0 评论 -
leetcode 19. Remove Nth Node From End of List 删除倒数第n个节点;
Given a linked list, remove the n-th node from the end of list and return its head.Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, t...原创 2018-08-06 16:38:21 · 120 阅读 · 0 评论 -
leetcode 86. Partition List 将val值把分为左右两部分;
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of t...原创 2018-08-06 16:13:36 · 116 阅读 · 0 评论 -
Leetcode 83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.Example 1:Input: 1->1->2Output: 1->2Example 2:Input: 1->1->2->3->3Output: 1-...原创 2018-08-06 15:23:47 · 73 阅读 · 0 评论 -
leetcode 92 Reverse Linked List II(反转m--n);
Reverse a linked list from position m to n. Do it in one-pass.Note: 1 ≤ m ≤ n ≤ length of list.Example:Input: 1->2->3->4->5->NULL, m = 2, n = 4Output: 1->4->3->2->5-...原创 2018-08-06 14:55:03 · 100 阅读 · 0 评论 -
(三)剑指offer55 删除链表中的重复的结点;
题目描述在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5思路:当两个结点相同的时候 找到第一个不重复的结点当不相同的时候,转移到第二个节点上; cur->next=deleteNode(cur->ne...原创 2018-09-01 23:36:57 · 108 阅读 · 0 评论