【LeetCode总结】——链表(Linked List)

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:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

思路两个链表求和,采用双指针遍历求和,求和结果可直接放入原始链表之一,然后输出即可,要考虑进位,可采用前置指针进行跟踪记录,并创建新的节点、分配内存存储进位,同时要考虑链表长度不一致的情况。

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:

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.

思路删除链表倒数第n个节点,采用双指针遍历,一个快指针,一个慢指针,他们间隔n,那么快指针遍历到最后一个节点时,他们中间的节点就是要删除的,注意只有一个节点且要删除的情况。

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:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

思路合并两个有序的链表,递归遍历两个链表,注意如果一个为空指针,那么返回另一个,同时这也是递归的出口。

24. Swap Nodes in Pairs

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3

思路两两交换链表节点,采用递归,设中间指针,注意出口条件。

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 = 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

思路:旋转链表,如果是空指针或者只有一个节点,则返回原来的节点,如果k值大于链表长度,那么k = k % n(k=4和k=1的效果一样),首先遍历链表求长度,然后将链表首尾链接,最后在n-k处断开,就得到想要的结果。

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->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

思路:删除链表中重复的节点(重复的只留一个),采用一个指针指向链表头节点,然后遍历判断并删除重复的节点。

86. Partition List

Given a linked list and a value x, partition it such that all nodes less than xcome before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

Example:

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5

思路:给一个链表,将小于值x的节点放到所有大于等于值x的结点的前面,不要改变结点之间的顺序。建立两个节点,分别连接大于x的值和小于x的z值,然后把两个节点相连即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值