1.知识点回顾

  https://www.cnblogs.com/BigJunOba/p/9174206.html

  https://www.cnblogs.com/BigJunOba/p/9174217.html

  2.典型例题(Easy)

  (1)707 Design Linked List

Implement these functions in your linked list class:

    get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
addAtHead(val) : Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
addAtTail(val) : Append a node of value val to the last element of the linked list.
addAtIndex(index, val) : Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid. Example: MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
linkedList.get(1); // returns 2
linkedList.deleteAtIndex(1); // now the linked list is 1->3
linkedList.get(1); // returns 3

  模型:Head→Node(0)→Node(1)→Node(2)→Node(3)→null(从第二题开始,都使用另外一种模型Head(0)→Node(1)→Node(2)→Node(3)

package LinkedList;

public class E707DesignLinkedList {

    class Node{
private int val;
private Node next; public Node(int val, Node next) {
this.val = val;
this.next = next;
} public Node(int val) {
this(val, null);
} } private Node head; /** Initialize your data structure here. */
public E707DesignLinkedList() {
head = new Node(0);
} /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {   // 定位到Node(index),判断Node(index)是否为null
Node p = head;
int i = -1;
while (p != null && i < index) {
i++;
p = p.next;
}
if (p == null || index < 0) return -1;
return p.val;
} /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
public void addAtHead(int val) {
Node p = new Node(val);
p.next = head.next;
head.next = p;
} /** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
Node p = head;
while (p.next != null) {
p = p.next;
}
Node q = new Node(val);
p.next = q;
} /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) { // 定位到Node(index-1),判断Node(index-1)是否为null
Node p = head;
int i = -1;
while (p != null && i < index-1) {
i++;
p = p.next;
}
if (p == null) return;
Node q = new Node(val);
q.next = p.next;
p.next = q;
} /** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) { // 定位到Node(index-2),判断Node(index-1)是否为null
Node p = head;
int i = -1;
while (p.next != null && i < index - 1) {
i++;
p = p.next;
}
if (p.next == null || index < 0) return;
p.next = p.next.next;
} // 求长度 head-1-2-3-4-5-null:返回5
   public int getlength() {
        int length = 0;
        Node p = head;
        while (p.next != null) {
            length++;
            p = p.next;
        }
        return length;
    } public void traverse() {
System.out.print("bianli: ");
Node p = head.next;
while (p != null) {
System.out.print(p.val + " ");
p = p.next;
}
System.out.println();
}
}

  (2)206.反转单链表

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

  堆栈方式(O(n) + O(n)):

    public ListNode reverseList(ListNode head) {
Stack<Integer> stack = new Stack<>();
ListNode p = head;
while (p != null) {
stack.push(p.val);
p = p.next;
}
ListNode result = head;
while (!stack.isEmpty()) {
result.val = stack.pop();
result = result.next;
}
return head;
}

  迭代方式(O(n) + O(1)):将1->2->3->4->5->NULL改成5->4->3->2->1->NULL

public ListNode reverseList(ListNode head) {
ListNode prev = null;   // 初始化当前结点(头结点)的前一个结点为null
ListNode curr = head;             // 初始化当前结点为头结点
while (curr != null) {            // 如果当前结点如果不为空
ListNode nextTemp = curr.next; //
curr.next = prev; // 当前结点指向当前结点的前一个结点
prev = curr; // 更新前一个结点为当前结点
curr = nextTemp; // 更新当前结点为当前结点的下一个结点
}
return prev; // 最后一步:1结点不为null,nextTemp为null,1结点指向2结点,prev为2结点,curr为null,返回prev
}

  递归方式(O(n) + O(n)):

public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head; // 出口条件,一直到5,才返回head为5结点
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
假设单链表的结构如下:
n1 → … → nk-1 → nk → nk+1 → … → nm → Ø
假设nk+1 → … → nm已经被颠倒过来并且现在在nk,那么必须nk.next.next = nk;也就是nk.next是nk+1,然后反向指就是nk+1.next又是nk,同时必须要使nk.next=null,因为第一个结点的next必须为null。

  分析递归过程:注意p不变,一直都是最后一个结点!!!为新链表的头结点。

0 Main:reverseList(1)
1 head:1,p=reverseList(2);
head:2,p=reverseList(3);
head:3,p=reverseList(4);
head:4,p=reverseList(5); ↑ →null return p=5
5 head:3,p=reverseList(4); 1→2→3→ 4 ← 5 ↑→null return p=5
6 head:2,p=reverseList(3); 1→2→3←4←5 ↑→null return p=5
7 head:1,p=reverseList(2); 1→2←3←4←5 ↑→null return p=5
8 Main:reverseList(1) 1←2←3←4←5 return p=5

  (3)876.求链表中间结点ListNode

Example 1:

Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL. Example 2: Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.

  注意:这里的头结点head是有数据的,以[1,2,3,4,5]为例,head.val的值是1

    public ListNode middleNode(ListNode head) {
int length = 0;
ListNode node = head;
while(node != null){
length++;
node = node.next;
}
ListNode result = head;
for(int i = 1; i <= length/2; i++){
result = result.next;
}
return result;
}

  (4)删除当前ListNode node的两种方法

1.定位到node
node.val = node.next.val;
node.next = node.next.next;
2.定位到node的前一个结点
node.next = node.next.next;

  (5)迭代方法删除单链表中val的值等于给定的val的结点

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

  迭代方法:

public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
head.next = removeElements(head.next, val);
return head.val == val ? head.next : head;
}

  非迭代方法:1->2->6->3->4->5->6, val = 6                  Output: 1->2->3->4->5

  总结,为了应对末尾的5→6→null,的问题,必须当curr为5时判断curr.next.val,然后才可以处理,也就是说定位到被删结点的前一个结点。

    public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
ListNode curr = head;
while(curr.next != null){ // 注意出口条件curr.next != null即可
if(curr.next.val == val){            // 注意在删完结点后,curr.next变成了被删结点的下一个结点,因此指针不用动
curr.next = curr.next.next;        // 如果出口条件是curr.next,那么循环内部最多只能多一个next,即curr.next.next
} else{
curr = curr.next;
}
}
return head.val == val ? head.next : head; // 处理这种情况:[1,2,...], 1,因为开始判断的时候就是curr.next即head.next,没有从head开始
}

  (6)删除val值重复的Node

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

  参考上面的(5)的非迭代方法可以很容易地写出来

   public ListNode deleteDuplicates(ListNode head) {
if (head == null) return null;
ListNode curr = head;
int prev = curr.val;
while (curr.next != null) {
if (curr.next.val == prev) {
curr.next = curr.next.next;
} else {
curr = curr.next;
prev = curr.val;
}
}
return head;
}

  迭代方法:

public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)return head;
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;
}

  (7)

  (8)

  (9)

  3.典型例题(Medium)

  4.典型例题(Hard)

Leetcode Tags(1)Linked List的更多相关文章

  1. Leetcode Tags(13)Bit Manipulation

    一.477.汉明距离总和 输入: , , 输出: 解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系) HammingDistance( ...

  2. Leetcode Tags(13)Tree

    1.前序.中序.后序递归方式遍历二叉树 public void preOrderRecur(Node T) { if (T != null) { System.out.print(T.val + &q ...

  3. Leetcode Tags(8)Binary Search

    一.475. Heaters 输入: [1,2,3],[2] 输出: 1 解释: 仅在位置2上有一个供暖器.如果我们将加热半径设为1,那么所有房屋就都能得到供暖. 输入: [1,2,3,4],[1,4 ...

  4. Leetcode Tags(6)Math

    一.204. Count Primes Count the number of prime numbers less than a non-negative number, n. Input: 10 ...

  5. Leetcode Tags(5)Hash Table

    一.500. Keyboard Row 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 输入: ["Hello", "Alaska", &q ...

  6. Leetcode Tags(3)String(TODO)

    一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 su ...

  7. Leetcode Tags(2)Array

    一.448. Find All Numbers Disappeared in an Array 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了 ...

  8. Leetcode Tags(4)Stack & Queue

    一.232. Implement Queue using Stacks private Stack<Integer> stack; /** Initialize your data str ...

  9. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

随机推荐

  1. Python连载40-协程定义及状态、send语句、yield用法

    一.协程 1.历史进程: (1)3.4引入协程,用yield来实现 (2)3.5引入协程语法 (3)实现协程比较好的包有asyncio,tornado,gevent 2.定义:协程是为非抢占式多任务产 ...

  2. JAVASE知识点总结(三)

    第十六章:抽象类和接口 一.抽象方法:在方法面前加了abstract(为了解决,子类必须要覆盖此方法,在定义的时候不要方法体). 特点:1.抽象方法没有方法体. 2.抽象方法必须放在抽象类(类前面加上 ...

  3. Hadoop核心组件之HDFS

    HDFS:分布式文件系统 一句话总结 一个文件先被拆分为多个Block块(会有Block-ID:方便读取数据),以及每个Block是有几个副本的形式存储 1个文件会被拆分成多个Block blocks ...

  4. JMeter 压测Server Agent无法监控资源问题,PerfMon Metrics Collector报Waiting for sample,Error loading results file - see file log, Can't accept UDP connections java.net.BindException: Address already in use 各种疑难杂症

    如何安装插件此博主已经说得很详细了. https://www.cnblogs.com/saryli/p/6596647.html 但是需注意几点: 1.修改默认端口,这样可以避免掉一个问题.Serve ...

  5. Cisco交换机、路由器,密码恢复

    一.路由器密码恢复 1.重启路由器,同时按下ctrl + breack键中断IOS的加载,路由器进入ROM Monitor模式 2.将配置寄存器的值更改为 0x2142,表示在启动时忽略startup ...

  6. 11 种在大多数教程中找不到的JavaScript技巧

    当我开始学习JavaScript时,我把我在别人的代码.code challenge网站以及我使用的教程之外的任何地方发现的每一个节省时间的技巧都列了一个清单. 在这篇文章中,我将分享11条我认为特别 ...

  7. 06-border

    边框 border:边框,描述盒子的边框 边框的三要素:粗细 线性样式 颜色 例如:border:1px solid red: 如果颜色不写,默认是黑色:如果粗细不写,不显示边框:如果只写线性样式,默 ...

  8. mysql操作遇到的坑(第一版)

    1.当我们要统计数据表数量时,如果遇到多表查询,会出现一个主表对应多个子表的维度,我们会用到group by,但是不要再用统计函数去操作数据,因为统计还是会统计原数据 案例 SELECT sum(`o ...

  9. Pycharm 快捷键大全 2019.2.3

    在Pycharm中打开Help->Keymap Reference可查看默认快捷键帮助文档,文档为PDF格式,位于安装路径的help文件夹中,包含MAC操作系统适用的帮助文档. 下图为2019. ...

  10. JDBC访问数据库的基本步骤

    加载驱动 通过DriverManager对象获取连接对象Connection 通过连接对象获取会话 通过会话进行数据的增删改查,封装对象 关闭资源