
LeetCode--Linked List
天天天青
这个作者很懒,什么都没留下…
展开
-
Delete Node in a Linked List(删除链表中的结点)
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with ...原创 2018-02-27 15:48:28 · 259 阅读 · 0 评论 -
Split Linked List in Parts(拆分链表)
Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list "parts".The length of each part should be as equal as possible: no two parts ...原创 2018-03-03 19:03:41 · 359 阅读 · 0 评论 -
Intersection of Two Linked Lists(找到两个链表的交叉结点)
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘ c...原创 2018-03-03 16:07:59 · 347 阅读 · 0 评论 -
Remove Linked List Elements(移除列表中指定的元素)
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5/** *...原创 2018-03-01 18:26:02 · 831 阅读 · 0 评论 -
Palindrome Linked List(回文链表)
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?思路:1、使用快慢指针,找到链表的中点;2、翻转中点之后的链表;3、从头结点开始,翻转的链表和未翻转的链表进行值的比较。/** * Definition for sing...原创 2018-03-01 11:40:05 · 247 阅读 · 0 评论 -
Linked List Cycle(链表是否有环)
思路:使用快、慢指针,当两个指针相等时,则链表有环。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution ...原创 2018-03-01 10:16:52 · 292 阅读 · 0 评论 -
Add Two Numbers(两个数相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i...原创 2018-03-01 09:44:19 · 255 阅读 · 0 评论 -
Remove Nth Node From End of List(删除从最后一个结点起的第n个结点)
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the en...原创 2018-03-08 15:14:08 · 358 阅读 · 0 评论 -
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 ...原创 2018-03-07 22:00:42 · 438 阅读 · 0 评论 -
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./** * D...原创 2018-02-27 16:08:17 · 357 阅读 · 0 评论 -
Merge Two Sorted Lists(合并两个有序的链表)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output: 1->1...原创 2018-02-27 16:04:24 · 275 阅读 · 0 评论 -
Reverse Linked List(逆链表)
Reverse a singly linked list.Hint:A linked list can be reversed either iteratively or recursively./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;...原创 2018-02-27 15:51:57 · 361 阅读 · 0 评论 -
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 pla...原创 2018-03-06 15:22:43 · 549 阅读 · 0 评论