链表
Wu_uuuu
这个作者很懒,什么都没留下…
展开
-
Leetcode-19. Remove Nth Node From End of List
题目删除链表的倒数第n个元素思路双指针p1, p2,p1先移动到链表的第n个元素,然后p1和p2一起移动,当p2到达末尾时,p1指向的元素即为要删除的元素的前一个元素。注意处理n大于链表长度的情况。代码class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { if(head ==原创 2017-03-04 21:37:31 · 198 阅读 · 0 评论 -
Leetcode-142. Linked List Cycle II
题目Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up: Can you solve it without using extra space? 如果链表中有环,返回环原创 2017-04-03 20:46:29 · 232 阅读 · 0 评论 -
Leetcode-138. Copy List with Random Pointer
题目A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list. 深拷贝一个链表,链表除了含有next指针外,还包含一个random指针原创 2017-04-03 20:26:01 · 302 阅读 · 0 评论 -
Leetcode-143. Reorder List
题目Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes’ values.For example, Given {1,2,3,4}, reorder it to {1,4,2,原创 2017-04-03 19:08:24 · 268 阅读 · 0 评论 -
求单链表的中间节点
题目 实现一个函数,返回单链表的中间节点,如果链表中节点个数为奇数,则返回中间节点,否则,返回中间两个节点中的任意一个。思路 设置快指针和慢指针,快指针走两步,慢指针走一步,当快指针走到末尾时,慢指针的位置即为中间节点。代码#include<iostream>using namespace std;typedef struct ListNode { int value;原创 2017-04-03 11:24:47 · 778 阅读 · 0 评论 -
Leetcode-141. Linked List Cycle
题目Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 判断链表中是否有环,不用额外存储空间思路最好的方法是时间复杂度O(n),空间复杂度O(1)的。设置两个指针,一个快一个慢,快的指针每次走两步,慢的指针每次走一步,如果快指针原创 2017-04-03 17:01:00 · 284 阅读 · 0 评论 -
Leetcode-234. Palindrome Linked List
题目 判断一个链表是不是回文思路 先寻找链表的中间节点,然后反转中间节点及以后的节点,得到一个反转链表,比较原链表和反转链表即可。时间复杂度O(n),空间复杂度O(1)代码class Solution {public: bool isPalindrome(ListNode* head) { if(NULL == head || NULL == head->nex原创 2017-04-03 16:26:15 · 309 阅读 · 0 评论 -
Leetcode-21. Merge Two Sorted Lists
题目按顺序合并两个排好序的链表,使用原来链表的节点代码class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* newList = new ListNode(0); ListNode *p3 = newList; while(原创 2017-03-04 20:49:06 · 179 阅读 · 0 评论 -
Leetcode-24. Swap Nodes in Pairs
题目Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may原创 2017-03-28 17:32:47 · 194 阅读 · 0 评论 -
Leetcode-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.For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1-原创 2017-03-27 19:30:03 · 302 阅读 · 0 评论 -
Leetcode-83. Remove Duplicates from Sorted List
题目Given a sorted linked list, delete all duplicates such that each element appear only once.For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 删除有序链表中重复的元素代码class Solution原创 2017-03-27 17:12:04 · 188 阅读 · 0 评论 -
Leetcode-86. Partition List
题目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原创 2017-03-20 19:43:39 · 239 阅读 · 0 评论 -
Leetcode-2. Add Two Numbers
题目翻译: 给你两个表示两个非负数字的链表。数字以相反的顺序存储,其节点包含单个数字。将这两个数字相加并将其作为一个链表返回。 输入: (2 -> 4 -> 3) + (5 -> 6 -> 4) 输出: 7 -> 0 -> 8 思路一遍循环相加联表对应的值,设置标志来处理进位操作。代码class Solution {public: ListNode* addTwoNumbers(ListN原创 2017-03-04 20:06:06 · 174 阅读 · 0 评论 -
Leetcode-206. Reverse Linked List
题目Reverse a singly linked list. 反转链表代码递归实现class Solution {public: ListNode* reverseList(ListNode* head) { if (head == NULL || head->next == NULL) return head; ListNode原创 2017-04-03 15:32:34 · 238 阅读 · 0 评论 -
Leetcode-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 pl原创 2017-04-03 12:05:36 · 271 阅读 · 0 评论 -
找出两个链表的第一个公共节点
题目输入两个链表,找出它们的第一个公共节点。链表定义如下:struct ListNode { int value; ListNode* next;};方法一:暴力查找 在第一个链表上顺序遍历每个节点,每遍历到一个节点的时候,在第二个链表上顺序遍历每个节点。如果第二个链表上的节点和第一个链表上的节点一样,就说明两个链表在节点上重合,于是就找到了公共的节点。但这种方法时间复杂度较高原创 2017-04-03 10:35:08 · 710 阅读 · 0 评论 -
Leetcode-25. Reverse Nodes in k-Group
题目Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of原创 2017-04-09 12:12:50 · 309 阅读 · 0 评论 -
Leetcode-92. Reverse Linked List II
题目Reverse a linked list from position m to n. Do it in-place and in one-pass.For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL. 反转链表中第m至第n个元素代码class Solution {public原创 2017-04-03 21:19:08 · 211 阅读 · 0 评论