链表习题

反转一个单链表。

反转一个单链表。

示例:

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

思路:
定义cur节点为当前节点,prev节点存储cur节点跳转到下一节前的节点,即先把cur存储到prev后再跳转下一节点,然后利用cur.next=prev.实现节点的反转。但这样最后一个节点无法反转,因此当curNext等于null时,让最后一个节点等于新的头节点,最后返回新的头节点,实现反转。
0L3dlX29wa24=,size_16,color_FFFFFF,t_70#pic_center)

重要代码片段如下:

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode prev = null;
        ListNode newHead = null;
        while( cur != null ){
             ListNode curNext = cur.next;
             if( curNext ==null ){
                 newHead = cur;
             }
             cur.next = prev;
             prev = cur;
             cur = curNext;
        }
        return newHead;
    }
}

链表的回文结构

链表的回文结构

对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。

给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。

思路:找到中间节点,然后从中间节点发转,然后判断中间节点的左右是否相等

代码

public class PalindromeList {
    public boolean chkPalindrome(ListNode A) {
        // write code here
        //1,找到中间位置
        ListNode slow = A;
        ListNode fast = A;
        while(fast !=null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        //2,从中间位置翻转
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        while(A != slow){
            if(A.val != slow.val){
                return false;
            }
            //偶数的情况
            if(A.next == slow){
                return true;
            }
             A = A.next;
            slow = slow.next;
        }
        return true;
    }
}

删除链表中重复的结点

删除链表中重复的结点

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        if (pHead == null) return null;
        ListNode newHead = new ListNode(-1);
        ListNode cur = pHead;
        ListNode tmp = newHead;
        while(cur != null){
        if(cur.next != null && cur.val == cur.next.val){
            while(cur.next != null && cur.val == cur.next.val){
                cur = cur.next;
            }
            cur = cur.next;
        }else{
            tmp.next = cur;
            tmp = tmp.next;
            cur = cur.next;
        }
        }
        tmp.next = null;
        return newHead.next;
    }
}

链表分割

现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
思路

(1)先令cur=head,把链表分成两段,第一段为小于目标值得,第二段为大于等于目标值的
(2)让cur遍历链表并判断节点放入哪一段里,直到cur==null;
(3)若cur.val<x,把cur尾插法到第一段里(分为是否第一次,如是第一次放进去就行了),若cur.val>=x,一样的方法
(4)循环结束后把第二段尾插到第一段最后就行了,返回bs
(5)最后要判断所有节点都在某一段的情况,若都在第二段,头结点就应是as
(6)在判断若第二段有节点,则要把第二段ae.next设为null,防止链表成环

代码

public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode cur = pHead;
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        while(cur != null){
            if(cur.val < x){
                //说明一个节点还没有
                if (bs ==null){
                bs = cur;
                be = cur;
            }else{
                be.next = cur;
                be = be.next;
            }
        }else{
                if(as == null){
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null){
            return as;
        }
        be.next = as;
        if(as != null){
            ae.next = null;
        }
        return bs;
    }
}

扁平化多级双向链表

多级双向链表中,除了指向下一个节点和前一个节点指针之外,它还有一个子链表指针,可能指向单独的双向链表。这些子列表也可能会有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示。

给你位于列表第一级的头节点,请你扁平化列表,使所有结点出现在单级双链表中。

原题链接:题目链接

解析:题目的意思其实就是把下级child的所有节点连接起来
两种思路:(具体解析写在注释中)

思路一:递归(因为child还有可能存在child节点)

代码:

class Solution {
    public Node flatten(Node head) {
        if(head == null) return null;
        Node cur = head;
        //向后遍历
        while (cur != null){
            while(cur.child != null){
                //先保留cur地next
                Node next = cur.next;
                //如果child还有child,连接起来
                Node child = flatten(cur.child);
                cur.next = child;
                child.prev = cur;
                cur.child = null;
                //把next连接起来
                if(next != null){
                    //child后面的节点
                    while(cur.next != null){
                        cur = cur.next;
                    }
                    //连接child最后的节点与next连接
                    cur.next = next;
                    next.prev = cur;
                }
            }
            cur = cur.next;
        }
        return head;
    }
}

思路二:运用栈存储每一个节点后面的next节点(具体解析写在注释中)

class Solution {
    public Node flatten(Node head) {
        if(head == null) return null;
        //定义栈存储节点
        Stack<Node> stack = new Stack<>();
        Node cur = head;
        while(true){
            if(cur.child != null){
                //将next存储在栈中
                if(cur.next != null){
                    stack.push(cur.next);
                }
                //连接cur的child节点
                cur.next = cur.child;
                cur.child.prev = cur;
                cur.child = null;
                //代码执行到这里如果cur.child还有child的话会在循环里继续执行,
            }
            if(cur.next != null){
                cur = cur.next;
            }else if (!stack.isEmpty()){
                //栈里有节点说明现在的cur在子链表上最后一个节点
                 Node next = stack.pop();
                 cur.next = next;
                 next.prev = cur;
                 cur = next;
            }else{
                //栈里没有节点,走到了主链的最后
                return head;
            }
        }
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

we_opkn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值