コード例 #1
0
 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;
 }
コード例 #2
0
 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;
 }
コード例 #3
0
 private int Count(ListNode head)
 {
     if (head == null)
         return 0;
     int count = 0;
     while (head != null)
     {
         count++;
         head = head.next;
     }
     return count;
 }
コード例 #4
0
 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;
     }
 }
コード例 #8
0
 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;
 }
コード例 #11
0
 public void DeleteNode(ListNode node)
 {
     node.val = node.next.val;
     node.next = node.next.next;
 }