算法——链表

算法——链表

  1. 判断回文链表
    方法一、使用栈
public class Day3 {
    public static class ListNode{
        int val;
        ListNode next;
        ListNode(int x){
            val = x;
        }
    }
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode temp = head;
        ListNode temp1 =head;
        int flag = 1;
        head.next = new ListNode(2);
        head = head.next;
        head.next = new ListNode(3);
        head = head.next;
        head.next = new ListNode(2);
        head = head.next;
        head.next = new ListNode(1);
        Stack<ListNode> stack = new Stack<>();
        while(temp!=null){
            stack.push(temp);
            temp = temp.next;
        }
        while(!stack.isEmpty()){
            if(stack.pop().val==temp1.val){
                temp1 = temp1.next;
            }else{
                flag=0;
            }
        }
        if(flag==1) System.out.println(true);
        else System.out.println(false);
    }
}

方法二、栈优化,使用快慢指针

public class Day3 {


    public static class ListNode{
        int val;
        ListNode next;
        ListNode(int x){
            val = x;
        }
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode temp = head;//快指针
        ListNode temp0=head;//慢指针
        ListNode temp1 =head;
        int flag = 1;
        head.next = new ListNode(2);
        head = head.next;
        head.next = new ListNode(3);
        head = head.next;
        head.next = new ListNode(2);
        head = head.next;
        head.next = new ListNode(1);
        Stack<ListNode> stack = new Stack<>();

        while( temp.next!=null && temp.next.next!=null){
        //奇数偶数的快慢指针
            temp = temp.next.next;
            temp0 = temp0.next;//3 3 2 1
        }
        while(temp0!=null){
            stack.push(temp0);
            temp0=temp0.next;
        }
        while(!stack.isEmpty()){
            if(stack.pop().val==temp1.val){
                temp1 = temp1.next;
            }else{
                flag=0;
            }
        }
        if(flag==1) System.out.println(true);
        else System.out.println(false);
    }
}

方法三、使用快慢指针加链表反转进行比较

public class Day3 {


    public static class ListNode{
        int val;
        ListNode next;
        ListNode(int x){
            val = x;
        }
    }

    public static ListNode reverse(ListNode head){
        ListNode cur = head;
        ListNode pre = null;
        while(cur!=null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode temp = head;//快指针
        ListNode temp0=head;//慢指针
        ListNode temp1 =head;
        int flag = 1;
        head.next = new ListNode(2);
        head = head.next;
        head.next = new ListNode(3);
        head = head.next;
        head.next = new ListNode(3);
        head = head.next;
        head.next = new ListNode(2);
        head = head.next;
        head.next = new ListNode(1);

        while(temp.next.next!=null){//偶数的快慢指针
            temp = temp.next.next;
            temp0 = temp0.next;//3 3 2 1
        }

        ListNode node = reverse(temp0.next);
        while(node != null){
            if(temp1.val != node.val){
                flag = 0;
                break;
            }
            node = node.next;
            temp1 = temp1.next;
        }
        if(flag==1) System.out.println(true);
        else System.out.println(false);
    }
}
  1. 将单向链表按照某个值分成左边小、中间相等、右边大的形式
public class Day4 {

    public static class ListNode{
        int val;
        ListNode next;
        ListNode(int x){
            val = x;
        }
    }

    public static ListNode partition(ListNode head,int target){
        ListNode SH = null;
        ListNode ST = null;
        ListNode EH = null;
        ListNode ET = null;
        ListNode BH = null;
        ListNode BT = null;
        while(head != null){
            ListNode next = head.next;
            head.next = null;
            if(head.val<target){
                if(SH==null){
                    SH = head;
                    ST = head;
                }else{
                    ST.next = head;
                    ST = head;
                }
            }else if(head.val == target){
                if(EH == null){
                    EH = head;
                    ET = head;
                }else{
                    ET.next = head;
                    ET = head;
                }
            }else{
                if(BH == null){
                    BH = head;
                    BT = head;
                }else{
                    BT.next = head;
                    BT = head;
                }
            }
            head = next;
        }
        if (ST != null){
            ST.next = EH;
            ET = ET == null ? ST : ET;
        }
        if (ET != null){
           ET.next = BH;
        }
        return SH != null ? SH : (EH != null ? EH : BH);
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode temp = head;
        head.next = new ListNode(5);
        head = head.next;
        head.next = new ListNode(3);
        head = head.next;
        head.next = new ListNode(2);
        head = head.next;
        head.next = new ListNode(1);
        ListNode node = partition(temp,3);
        while(node!=null){
            System.out.println(node.val);
            node = node.next;
        }//12135
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值