首页 > 其他分享 >4、在链表中穿针引线

4、在链表中穿针引线

时间:2023-05-11 09:15:02浏览次数:50  
标签:head ListNode cur next 链表 public 穿针引线

1、反转链表

206 - 反转链表

image

/**
 * 非递归实现
 * 最终状态如下
 * cur、next -> null
 * prev -> newHead
 */
public static ListNode reverseList1(ListNode head) {
    ListNode prev = null;
    ListNode cur = head;
    ListNode next;
    while (cur != null) {
        next = cur.next;
        cur.next = prev;
        prev = cur;
        cur = next;
    }

    return prev;
}

/**
 * 递归: 反转以 head 为头的链表, 并返回新的头结点
 */
public static ListNode reverseList2(ListNode head) {
    if (head == null || head.next == null) return head;

    ListNode newHead = reverseList2(head.next);
    head.next.next = head;
    head.next = null;

    return newHead;
}

更多问题
92 - 反转链表 II

2、测试你的链表程序

public class ListNode {

    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this(val, null);
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public ListNode(int[] arr) {
        if (arr == null || arr.length == 0) throw new IllegalArgumentException("Arr is empty.");

        this.val = arr[0];
        ListNode cur = this;
        for (int i = 1; i < arr.length; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        ListNode cur = this;
        while (cur != null) {
            sb.append(cur.val).append("->");
            cur = cur.next;
        }
        sb.append("NULL");
        return sb.toString();
    }
}

3、删除排序链表中的重复元素

83 - 删除排序链表中的重复元素

标签:head,ListNode,cur,next,链表,public,穿针引线
From: https://www.cnblogs.com/lidong422339/p/17389958.html

相关文章

  • 2023-05-10:给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表 如果在二叉
    2023-05-10:给你一棵以root为根的二叉树和一个head为第一个节点的链表如果在二叉树中,存在一条一直向下的路径且每个点的数值恰好一一对应以head为首的链表中每个节点的值,那么请你返回True否则返回False。一直向下的路径的意思是:从树中某个节点开始,一直连续向下的路径......
  • 回文链表
    /方法一:反转链表逐个比较/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode(intx):val(x),next(NULL){}*};*///classSolution{//public://boolisPalindrome(ListNode*head){//ListNo......
  • 单链表——追加函数(有无懂的大佬解答一下why不加强制类型过不去)
    #include<bits/stdc++.h>usingnamespacestd;typedefstruct{intid;stringname;}Data;typedefstruct{ DatanodeData; structNode*nextNode;}CLtype;//追加链表CLtype*CLAddEnd(CLtype*head,Datanodedata){CLtype*node,*htemp; if(!(node=(CLt......
  • 双链表和队列-->gcc编译
    双链表队列doublueList.h#include<stdlib.h>#include<stdio.h>#include<assert.h>#include<stdbool.h>typedefintLTDataType;typedefstructDList{ LTDataTypedata; structDList*next; structDList*prev;}LTNode;LTNode*init();......
  • 5-9打卡:力扣19. 删除链表的倒数第 N 个结点
    给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。 示例1:输入:head=[1,2,3,4,5],n=2输出:[1,2,3,5]示例2:输入:head=[1],n=1输出:[]示例3:输入:head=[1,2],n=1输出:[1] 提示:链表中结点的数目为sz1<=sz<=300<=Node.val<=1001<=n<=sz......
  • 环形链表
     /** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    bool hasCycle(ListNode......
  • 链表中倒数最后k个结点
    描述输入一个长度为n的链表,设链表中的元素的值为ai ,返回该链表中倒数第k个节点。如果该链表长度小于k,请返回一个长度为0的链表。  数据范围:0≤n≤10^5,0≤ai​≤10^9,0≤k≤10^9要求:空间复杂度 O(n),时间复杂度 O(n)进阶:空间复杂度 O(1),时间复杂度 O(n) 例......
  • java链表的疑惑
    head.next=tail; tail=newListNode();为什么head.next不等于tail在cpp里面head->next=tail;tail=newListNode();这时head->next==tail.这是因为head->next存放的是tail的地址,而java中head.next=tail; tail=newListNode();head.next存放的是tail的之前......
  • 四种语言刷算法之环形链表 II
    力扣142. 环形链表II1、C/***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;*};*/structListNode*detectCycle(structListNode*head){if(head==NULL||head->next==NULL)returnNULL;stru......
  • (第26章)LinuxC本质中链表、二叉树和哈希表
    文章目录一、单链表的结构决定只能出栈,入栈1.链表的结构2.链表与数组的区别3.单链表所有基本操作代码(1)链表的插入(2)链表的查找(3)链表的删除(3)遍历整个链表(4)销毁整个链表4.习题5.C++NULL指针二、双向链表结构决定可以出队和入队1.在上面的单项链表上改改,得到双向链表2.改进双向链表:新增......