《算法通关村第二关-链表反转的拓展问题解析》

1.指定区间反转

1.1 头插法

public ListNode reverseBetween(ListNode head, int left, int right) {
    ListNode dummyNode = new ListNode(-1);
    dummyNode.next = head;
    ListNode pre = dummyNode;

    for(int i = 0; i < left - 1; i++) {
        pre = pre.next;
    }

    ListNode cur  = pre.next;

    for(int i = 0; i < right - left; i++) {
        ListNode next = cur.next;
        cur.next = next.next;
        next.next = pre.next;
        pre.next = next;
    }
    return dummyNode.next;
}

1.2穿针引线法

public ListNode reverseBetween(ListNode head, int left, int right) {
    ListNode dummy = new ListNode(-1);
    dummy.next = head;
    ListNode pre = dummy;
    for(int i = 0; i < left - 1; i++) {
        pre = pre.next;
    }

    ListNode start = pre.next;
    ListNode end = start;
    for(int i = left; i < right; i++) {
        end = end.next;
    }

    ListNode succ = end.next;

    end.next = null;

    reverseList(start);

    pre.next = end;
    start.next = succ;

    return dummyNode.next;
}

public void reverseList(ListNode node) {
    ListNode  pre = null;
    ListNode cur = head;

    while(cur != null) {
        ListNode next = cur.next;
        cur.next = pre;
        pre = cur;
        cur = next;
    }
}

2.两两交换链表中的节点

public ListNode swapPairs(ListNode head) {
    ListNode dummyHead = new ListNode(0);
    dummyHead.next = head;
    ListNode tmp = dummyHead;

    while(tmp.next != null && tmp.next.next != null) {
        ListNode node1 = tmp.next;
        ListNode node2 = tmp.next.next;

        tmp.next = node2;
        node1.next = node2.next;
        node2.next = node1;
        tmp = node1;
    }
    return dummyHead.next;
}

3 单链表加1

public ListNode plusOne(ListNode head) {
    Stack<Integer> st = new Stack();
    while(head != null) {
        st.push(head.val);
        head = head.next;
    }
    int carry = 0;
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    int adder = 1;
    while(!st.empty() || carray > 0) {
        int digit = st.empty() ? 0 : st.pop();
        int sum = digit + adder + carry;
        carry = sum >= 10 ? sum - 10 : sum;
        ListNode cur = new ListNode(sum);
        cur.next = dummy.next;
        dummy.next = cur;
        adder = 0;
    }

    return dummy.next;
}

4 链表加法

public static ListNode addInListByStack(ListNode head1, ListNode head2) {
    Stack<ListNode> st1 = new Stack<ListNode>();
    Stack<ListNode> st2 = new Stack<ListNode>();
    while(head1 != null) {
        st1.push(head1);
        head1 = head1.next;
    }
    while(head2 != null) {
        st2.push(head2);
        head2 = head2.next;
    }
    ListNode newHead = new ListNode(-1);
    int carry = 0;
    while(!st1.empty() || !st2.empty() || carry != 0) {
        ListNode a = new ListNode(0);
        ListNode b = new ListNode(0);
        if (!st1.empty()) {
            a = st1.pop();
        }
        if(!st2.empty()) {
            b = st2.pop();
        }
        int get_sum = a.val + b.val + carry;
        int ans = get_sum % 10;
        carry = get_sum / 10;
        ListNode cur = new ListNode(ans);
        cur.next = newHead.next;
        newHead.next = cur;
    }
    return newHead.next;
}

5.再论链表的回文序列问题

public boolean isPalindrome(ListNode head) {
    if(head == null || head.next == null) {
        return true;
    }

    ListNode slow = head, fast = head;
    ListNode pre = head, prepre = null;
    while(fast != null && fast.next != null) {
        pre = slow;
        slow = slow.next;
        pre.next = prepre;
        prepre =pre;
    }

    if(fast != null) {
        slow = slow.next;
    }
    while(pre != null && slow != null) {
        if(pre.val != slow.val) {
            return false;
        }
        pre = pre.next;
        slow = slow.next;
    }
    return true;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值