Leetcode
206. Reverse Linked List
Reverse a singly linked list.
解题思路:
翻转链表,三个指针pre, cur和next指针
|
|
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.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
解题思路:
画图!画图!画图!
搞清链表的拼接处的节点。
|
|
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.
解题思路:
去除排序链表中重复的节点
|
|
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 the two partitions.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
解题思路:
拼接两条链表的操作,cur节点从虚拟头节点开始!
|
|
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 place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example:
|
|
Note:
|
|
解题思路:
和上一道题目一模一样
|
|
2. 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 it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example
|
|
解题思路:
把两条链表对应的值相加,得到的值的个位数创建新的节点,保留进位;
注意循环退出的条件
|
|
445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
|
|
解题思路:
本道题目与上一题是类似的,只不过对于输入和输出都需要先翻转链表
然而题目步允许翻转链表,借助两个栈进行输入的翻转,
输出的链表使用往前插入,达到链表翻转的目的。
|
|
203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.
Example:
|
|
解题思路:
删除链表中特定的值,一个一个遍历,很简单;
注意更新pre节点的时机
|
|
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:
|
|
解题思路:
输入的链表是排序链表;
重复的节点全部删除,注意pre的处理
|
|
21. 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:
|
|
解题思路:
合并链表
|
|
解法二:
递归实现:用心去感受,难以说明
|
|
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 not modify the values in the list, only nodes itself can be changed.
解题思路:
四个指针,pre, node1, node2 和 next
|
|
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 nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
example:
|
|
解题思路:
更新两个关键的节点,
一个是待翻转链表的前一个节点(PreOfReverse)
一个是链表翻转之后的尾节点,即未翻转链表的头节点
|
|
147. Insertion Sort List
Sort a linked list using insertion sort.
解题思路:
使用插入排序,每次取旧队列头节点,插入到新队列的合适位置,pre.val <= cur.val
|
|
148. Sort List
Sort a linked list in O(n log n) time using constant space complexity.
解题思路:
归并排序链表
|
|
237. 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 value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
解题思路:
删除指定的节点,修改节点值
|
|
19. Remove Nth Node From End of List
Given a linked list, remove the n(th) node from the end of list and return its head.
Example:
|
|
Note:
Given n will always be valid.
Try to do this in one pass.
解题思路:
双指针问题,注意preOfRemoveNode的位置,画图,枚举
|
|
61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.
Example:
|
|
解题思路:
其实问题挺简单的,就是preOfRotateNode要找对了,还有节点tail
|
|
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,3}.
解题思路:
分割链表,并重装
|
|
234. 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?
解题思路:
解法与上一题一样
|
|