单链表问题(反转、是否有环、删除结尾第N个节点、合并两个sortlist、找到交点)

简介: 1.时间复杂度O(N),内存O(1)的效率下实现单链表的翻转public static TreeNode revers(TreeNode head){ TreeNode temp,first,second; first=head; second=head.next; while(second!=null){ temp=second.next; second.n

1.时间复杂度O(N),内存O(1)的效率下实现单链表的翻转

public  static TreeNode revers(TreeNode head){
		TreeNode temp,first,second;
		first=head;
		second=head.next;
		while(second!=null){
			temp=second.next;
			second.next=first;
			first=second;
			second=temp;
		}
		head.next=null;
		head=first;
		return head;
		
	}


2.判断一个链表是否存在环,采用的方法是一个指针按照补偿为1遍历,另一个按照步长为2遍历,如果重合说明有环。

public class IfHasCircle {
    public static boolean ifhascircle(TreeNode head){
    	   TreeNode first=head;
    	   TreeNode second=head;
    	   while(first!=null && second!=null){
    		   if(first==second){
    			   return true;
    		   }
    		   first=first.next;
    		   second=second.next.next;
    	   }
    	   return false;
    }


3.删除从结尾处数第n个节点。思路是做两个指针,一个步长比另一个长n,这样当长的那个节点遍历到最后一个的时候,就直接把短的节点删除即可。

	public  static TreeNode DelTheLastN(TreeNode head,int n){
		TreeNode first,second;
		first=head;
		second=head;
		for(int i=1;i<=n;i++){
			second=second.next;
		}
		while(second.next!=null){
			second=second.next;
			first=first.next;
		}
		first.next=first.next.next;
		return head;
		
	}


4.合并两个sort list:用递归

 static TreeNode mergTwoSortList(TreeNode l1,TreeNode l2){
    	    if(l1 == null) return l2;
    	    if(l2 == null) return l1;

    	    if(l1.val < l2.val) {
    	        l1.next = mergTwoSortList(l1.next, l2);
    	        return l1;
    	    } else {
    	        l2.next = mergTwoSortList(l2.next, l1);
    	        return l2;
    	    }
      }

5. 两个链表相交,找出交点

求出两个链表的长度a和b,一个指针指向较短链表的头head,另一个指针指向较长链表的第head+|a-b|,然后两个指针一起移动,相遇处即为交点。


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null || headB==null) return null;
      
        int Aindex_length=getLength(headA);
        int Bindex_length=getLength(headB);
        int dis=Math.abs(Aindex_length-Bindex_length);
        
        
            
        
        ListNode Aindex=headA;
        ListNode Bindex=headB;
        if(Aindex_length>=Bindex_length){
          for(int i=0;i<dis;i++){
              Aindex=Aindex.next;
          }  
          while(Bindex!=null){
              if(Aindex.val==Bindex.val){return Aindex;}
              else{
                  Aindex=Aindex.next;
                  Bindex=Bindex.next;
                 }
          }
        }
       
         Aindex=headA;
         Bindex=headB;
      if(Aindex_length<Bindex_length){
          for(int i=0;i<dis;i++){
              Bindex=Bindex.next;
          }  
          while(Aindex!=null){
              if(Aindex.val==Bindex.val){return Aindex;}
              else{
                  Aindex=Aindex.next;
                  Bindex=Bindex.next;
                 }
          }
        }
        
        
        return null;
    }
    public int getLength(ListNode head){
        ListNode index=head;
        int length=1;
        while(index.next!=null){
            index=index.next;
            length++;
        }
        return length;
    }
}



/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/


目录
相关文章
|
8月前
删除有序链表中重复的元素-II(链表)
双指针,slow和fast,并且增加标记flag初始为1。
36 0
|
8月前
|
存储 算法
数组算法:倒置,查找,插入,删除
数组算法:倒置,查找,插入,删除
68 0
|
10月前
剑指offer_链表---删除链表中重复的结点
剑指offer_链表---删除链表中重复的结点
29 0
|
10月前
剑指offer 17. 删除链表中重复的节点
剑指offer 17. 删除链表中重复的节点
40 0
二叉查找树的建立,删除,非递归和递归查找给定元素,非递归和递归查找最大元素结点和最小元素结点
二叉查找树的建立,删除,非递归和递归查找给定元素,非递归和递归查找最大元素结点和最小元素结点
|
算法
Acwing 29.删除链表中的重复值(结点)
Acwing 29.删除链表中的重复值(结点)
69 0
9.删除链表中的重复结点
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
48 0
单链表任意位置插入
任意位置插入,第一个数据节点为0号下标
每日三题-合并两个有序链表、相交链表、删除链表的第N个节点
每日三题 删除链表的倒数第N个结点 合并两个有序链表 相交链表
67 3
每日三题-合并两个有序链表、相交链表、删除链表的第N个节点
LeetCode19删除链表的倒数第N个节点&20有效的括号
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
71 0
LeetCode19删除链表的倒数第N个节点&20有效的括号

热门文章

最新文章