【无标题】

 

      指针问题与递归问题比较重要。记录几个比较有意思的解法。

         三指针反转链表。

public static ListNode reverseList(ListNode head){
        if(head == null) return null;
        if(head.next == null) return head;
        if(head.next.next == null){
            ListNode temp = head.next;
            head.next = null;
            temp.next = head;
            return temp;
        }
        ListNode First = head;
        ListNode Second = head.next;
        ListNode Third = Second.next;
        First.next = null;
        while (Third.next != null){
            Second.next = First;
            First = Second;
            Second = Third;
            Third = Second.next;
        }
        Second.next = First;
        Third.next = Second;
        return Third;
    }

        递归反转链表。

    ListNode alist;
    public ListNode reverseList(ListNode head) {
        if(head == null){
            return null;
        }
        alist = new ListNode();
        ListNode T = alist;
        FZ(head);
        ListNode l = alist;
        while(l != head){
            l=l.next;
        }
        l.next = null;
        return T.next;
    }
    void FZ(ListNode head){
        if(head.next == null){
            alist.next = head;
            alist = alist.next;
            return;
        }
        FZ(head.next);
        alist.next = head;
        alist = alist.next;
    }

        其中最后一次反转会产生环,需要注意一下。

  1.         递归删除链表中的倒数N个节点。
    1.     public ListNode removeNthFromEnd(ListNode head, int n) {
              int i = remove(head,n);
              if(i==n-1){
                  if(head.next==null){
                      return null;
                  }else{
                      return head.next;
                  }
              }
              return head;
          }
          public int remove(ListNode Head , int lnum){
              if(Head.next == null){
                  return 0;
              }
              int num = remove(Head.next,lnum)+1;
              if(lnum == num){
                  Head.next = Head.next.next;
              }
              return num;
          }

           还有一个回溯算法中的题,单词搜索,这里写一下我的解法。

      1. public boolean exist(char[][] board, String word){
                ch = board;
                W = word;
                for(int y = 0 ; y < board.length ; y++){
                    for(int x = 0 ; x < board[0].length ; x++){
                        if (board[y][x] == word.charAt(0)){
                            it = new int[board.length][board[0].length];
                            it[y][x] = 1;
                            mymth(x,y,1);
                        }
                    }
                }
                return B;
            }
            char[][] ch;
            int [][] it;
            String W;
            Boolean B = false;
            public void mymth(int x ,int y , int index){
        
                if(index == W.length()){
                    B = true;
                    return;
                }
                if(x+1<ch[0].length &&it[y][x+1]==0&& ch[y][x+1] == W.charAt(index)){
                    it [y][x+1] = 1;
                    mymth(x+1 , y , index+1);
                    it[y][x+1] = 0;
                }
                
                if(x>0 && it[y][x-1] ==0 && ch[y][x-1] == W.charAt(index)){
                    it [y][x-1] = 1;
                    mymth(x-1 , y , index+1);
                    it[y][x-1] = 0;
                }
                
                if(y+1 <ch.length && it[y+1][x] == 0 && ch[y+1][x] == W.charAt(index)){
                    it [y+1][x] = 1;
                    mymth(x , y+1 , index+1);
                    it[y+1][x] = 0;
                }
                
                if(y>0 && it[y-1][x] == 0 && ch[y-1][x] == W.charAt(index)){
                    it [y-1][x] = 1;
                    mymth(x , y-1 , index+1);
                    it[y-1][x] = 0;
                }
            }

         回溯算法对递归的应用有点难度,要多思考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值