public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
 {
     if (headA == null || headB == null)
         return null;
     var countA = Count(headA);
     var countB = Count(headB);
     if (countA > countB)
         for (int i = 0; i < countA - countB; i++)
             headA = headA.next;
     else if (countA < countB)
         for (int i = 0; i < countB - countA; i++)
             headB = headB.next;
     ListNode result = null;
     while (headA != null)
     {
         if (headA.val == headB.val)
         {
             if (result == null)
                 result = headA;
         }
         else
             result = null;
         headA = headA.next;
         headB = headB.next;
     }
     return result;
 }
 public ListNode RemoveNthFromEnd(ListNode head, int n)
 {
     ListNode t = head;
     ListNode p = head;
     if (head == null)
         return null;
     else if (head.next == null && n > 0)
         return null;
     for (int i = 0; i < n - 1; i++)
         t = t.next;
     if (t.next == null)
         return head.next;
     else
         t = t.next;
     while (true)
     {
         if (t.next == null)
         {
             if (p.next == head)
                 return head.next;
             else
                 p.next = p.next.next;
             break;
         }
         else
         {
             p = p.next;
             t = t.next;
         }
     }
     return head;
 }
 private int Count(ListNode head)
 {
     if (head == null)
         return 0;
     int count = 0;
     while (head != null)
     {
         count++;
         head = head.next;
     }
     return count;
 }
 public ListNode DeleteDuplicates(ListNode head)
 {
     if (head == null)
         return null;
     var node = head;
     while (node.next != null)
     {
         if (node.val == node.next.val)
             node.next = node.next.next;
         else
             node = node.next;
     }
     return head;
 }
예제 #5
0
 public bool HasCycle(ListNode head)
 {
     if (head == null || head.next == null)
         return false;
     var slowpoint = head;
     var fastpoint = head;
     while (true)
     {
         if (fastpoint.next != null && fastpoint.next.next != null)
         {
             fastpoint = fastpoint.next.next;
             slowpoint = slowpoint.next;
             if (fastpoint == slowpoint)
                 return true;
         }
         else
             return false;
     }
 }
예제 #6
0
 private bool Check(ListNode current, ref ListNode head, ref bool isover)
 {
     //start to compare, it's the end of the linkedlist
     if (current == null)
         return true;
     var result = Check(current.next, ref head, ref isover);
     if (result == false)
         return false;
     else if (isover)
         return true;
     else if (current.val != head.val)
         return false;
     else
     {
         if (head == current || head.next == current)
             isover = true;
         else
             head = head.next;
         return true;
     }
 }
예제 #7
0
 public ListNode ReverseList(ListNode head)
 {
     if (head == null || head.next == null)
         return head;
     else
     {
         var p1 = head;
         var p2 = head.next;
         var p3 = head.next.next;
         p1.next = null;
         while (p2 != null)
         {
             p2.next = p1;
             if (p3 == null)
                 break;
             p1 = p2;
             p2 = p3;
             p3 = p2.next;
         }
         return p2;
     }
 }
 public ListNode RemoveElements(ListNode head, int val)
 {
     while (head != null && head.val == val)
         head = head.next;
     if (head == null)
         return null;
     else if (head.next == null)
         return head;
     else
     {
         var p = head;
         while (p.next.next != null)
         {
             if (p.next.val == val)
                 p.next = p.next.next;
             else
                 p = p.next;
         }
         if (p.next.val == val)
             p.next = null;
         return head;
     }
 }
예제 #9
0
 public bool IsPalindrome(ListNode head)
 {
     bool isover = false;
     return Check(head, ref head, ref isover);
 }
예제 #10
0
 public ListNode(int x)
 {
     val = x;
     next = null;
 }
 public void DeleteNode(ListNode node)
 {
     node.val = node.next.val;
     node.next = node.next.next;
 }