leetcode_linklist3
continue..
反转链表
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 = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL
Example 2:
Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL
/**
- Definition for singly-linked list.
- struct ListNode {
int val;
struct ListNode *next;
- };
*/
1 | struct ListNode* rotateRight(struct ListNode* head, int k) { |
移除倒数第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, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Follow up:
Could you do this in one pass?
/**
- Definition for singly-linked list.
- struct ListNode {
int val;
struct ListNode *next;
- };
*/
1 | struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { |
//一开始未考虑到删除头的情况,所以加了else 部分
交换元素
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 constant extra space.
You may not modify the values in the list's nodes, only nodes itself may be changed.
/**
- Definition for singly-linked list.
- struct ListNode {
int val;
struct ListNode *next;
- };
*/
1 | struct ListNode* swapPairs(struct ListNode* head) { |
k组反转
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.
Example:
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
Note:
Only constant extra memory is allowed.
You may not alter the values in the list's nodes, only nodes itself may be changed.
/**
- Definition for singly-linked list.
- struct ListNode {
int val;
struct ListNode *next;
- };
/
/
这道题目原想在遍历一次就解决,可是还是想不出好的方法,得遍历两次,一次计算长度,一次正真的处理;
在处理的过程使用头插法。注意边界条件,但因为有数量控制好些*/
1 | struct ListNode* reverseKGroup(struct ListNode* head, int k) { |
总结
后面还有几道题,不贴了,这里简述下:
检查是否链表中存在循环
检查链表中是否存在循环并找到循环的起点
深度复制链表,链表中的每个节点存在一个指向任意节点的指针
设计一个LRU cache,即(最近使用的)
。。。。。
链表的套路:
- 常使用头插法进行反转操作
- 使用两个指针解决循环问题,指针相遇测试是否存在循环,指针遍历减少时间
- 使用哈希,以空间换时间
- 加头节点,简化逻辑
使用链表注意
- 检查空和是否只有一个节点
- 释放空间,和放置取空指针,可以通过次数控制和判空