当前位置: 首页>>代码示例>>C#>>正文


C# ListNode类代码示例

本文整理汇总了C#中ListNode的典型用法代码示例。如果您正苦于以下问题:C# ListNode类的具体用法?C# ListNode怎么用?C# ListNode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ListNode类属于命名空间,在下文中一共展示了ListNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RemoveNthFromEnd

 public ListNode<int> RemoveNthFromEnd(ListNode<int> head, int n)
 {
     // assume the input is always valid
     if (head.Next == null) // only 1 node
     {
         return null;
     }
     // reach that node first
     var tail = head;
     var preDelete = head;
     for (int i = 0; i < n; i++)
     {
         tail = tail.Next;
     }
     if (tail == null) // toDelete is head
     {
         return head.Next;
     }
     while (tail.Next != null)
     {
         tail = tail.Next;
         preDelete = preDelete.Next;
     }
     preDelete.Next = preDelete.Next.Next;
     return head;
 }
开发者ID:dullcat,项目名称:leetcode_csharp,代码行数:26,代码来源:Q019_RemoveNthNodeFromEndofList.cs

示例2: ReverseSingleSublistTest

        public void ReverseSingleSublistTest()
        {
            Func<ListNode<int>, int, int, ListNode<int>>[] functions = new Func<ListNode<int>, int, int, ListNode<int>>[]
            {
                ReverseSingleSublist.BruteForce,
                ReverseSingleSublist.SinglePass
            };

            for(int i = 0; i < 10; i++)
            {
                int[] data = ArrayUtilities.CreateRandomArray(10, 0, 10);
                for (int start = 1; start <= 10; start++)
                {
                    for(int end = start; end <= 10; end++)
                    {
                        ListNode<int>[] results = new ListNode<int>[functions.Length];

                        for (int j = 0; j < results.Length; j++)
                        {
                            ListNode<int> head = LinkedListUtilities.Initialize(data);
                            results[j] = functions[j](head, start, end);
                            Assert.IsTrue(LinkedListUtilities.AreEqual(results[0], results[j]));
                        }
                    }
                }
            }
        }
开发者ID:mmoroney,项目名称:Algorithms,代码行数:27,代码来源:ReverseSingleSublist.cs

示例3: AddTwoNumbers

 public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
 {
     var add1=Convert(l1);
     var add2=Convert(l2);
     var res=add1+add2;
     return Convert(res);
 }
开发者ID:gaufung,项目名称:LeetCodeCSharp,代码行数:7,代码来源:002AddTwoNumbers.cs

示例4: MergeTwoLists

 //method to merge two sorted lists
 private ListNode MergeTwoLists(ListNode l1, ListNode l2){
     if(l1==null||l2==null){
         if(l1 ==null) return l2;
         if(l2 ==null) return l1;
     }
     ListNode newHead,p,p1,p2;
     if(l1.val < l2.val){
         newHead = l1;
          p1 = l1.next;
          p2 = l2;
     }
     else{
         newHead = l2;
          p1 = l1;
          p2 = l2.next;
     }
        
      p = newHead;
     while(p1!=null && p2!=null){
         if(p1.val < p2.val){
             p.next = p1;
             p1 = p1.next;
         }
         else{
             p.next = p2;
             p2 = p2.next;
         }
         p=p.next;
     }
     //no need to use the while loop, directly append p1 or p2 to p
     //because rest nodes of p1 or p2 are already sorted
     if(p1!=null) p.next=p1;
     if(p2!=null) p.next=p2;
     return newHead;
 }
开发者ID:siagung,项目名称:Qilu-leetcode,代码行数:36,代码来源:A178.MergeTwoUnsortedLists.cs

示例5: ReverseBetween

    public ListNode ReverseBetween(ListNode head, int m, int n)
    {
        var length = n-m +1;

        if(m == 1){
            ListNode tail;
            var h = ReverseList(head, length, out tail);
            head.next = tail;
            return h;
        }
        else{
            var c = 2;
            var t = head;
            while(c != m){
                t = t.next;
                c++;
            }

            ListNode tail;
            var h = ReverseList(t.next, length, out tail);
            t.next.next = tail;
            t.next = h;

            return head;
        }
    }
开发者ID:WillFr,项目名称:train,代码行数:26,代码来源:Solution.cs

示例6: ReverseBetween

 public ListNode ReverseBetween(ListNode head, int m, int n) {
     if(head==null) return null; //handle edge case
     ListNode preHead = new ListNode(0); 
     preHead.next = head;
     ListNode start = preHead; //start will be the (m-1)th node in the list
     //advance start pointer to the (m-1)th node in the list
     int count = 1;
     while(start!=null && count<m){
         start = start.next;
         count++;
     }
     
     if(count<m) return head; //length of the list is smaller than m, edge case
     //end is the mth node in the list
     ListNode end = start.next; //node will be inserted between start and end
     ListNode curr = end.next;//curr is the node to be insert 
     
     //insert each node right after start, advance curr until count==n
     while(curr!=null && count<n){
         ListNode next = curr.next; //get the next node using curr.next
         curr.next = start.next; //insert curr right after start node
         start.next = curr;
         end.next = next; //point end to next node
         curr = next; //advance curr to next node
         count++;
     }
     return preHead.next; //preHead is the node before original head, so return preHead.next
     
 }
开发者ID:siagung,项目名称:Qilu-leetcode,代码行数:29,代码来源:A113.ReverseLinkedListII.cs

示例7: Print

 private static void Print(ListNode n)
 {
     if(n != null){
         Console.Write(n.val);
         Print(n.next);
     }
 }
开发者ID:WillFr,项目名称:train,代码行数:7,代码来源:Solution2.cs

示例8: SwapPairs

        public ListNode<int> SwapPairs(ListNode<int> head)
        {
            if (head == null || head.Next == null)
            {
                return head;
            }
            ListNode<int> preResult = new ListNode<int>(-9999); // helper node
            var pre = preResult;
            var n1 = head;
            var n2 = head.Next;
            var post = n2.Next;
            while (true)
            {
                pre.Next = n2;
                n2.Next = n1;
                n1.Next = post;
                if (post == null || post.Next == null)
                {
                    break;
                }
                pre = n1;
                n1 = post;
                n2 = n1.Next;
                post = n2.Next;
            }

            return preResult.Next;
        }
开发者ID:dullcat,项目名称:leetcode_csharp,代码行数:28,代码来源:Q024_SwapNodesinPairs.cs

示例9: ReverseBetween

 public ListNode<int> ReverseBetween(ListNode<int> head, int m, int n)
 {
     if (m == n)
     {
         return head;
     }
     // find pre-changing head
     ListNode<int> dummy = new ListNode<int>(0);
     dummy.Next = head;
     var preChaningHead = dummy;
     int changeLength = n - m;
     while (m-- > 1)
     {
         preChaningHead = preChaningHead.Next;
     }
     var curNode = preChaningHead.Next;
     for (int i = 0; i < changeLength; i++)
     {
         var preNext = preChaningHead.Next;
         var curNext = curNode.Next;
         curNode.Next = curNext.Next;
         preChaningHead.Next = curNext;
         curNext.Next = preNext;
     }
     return dummy.Next;
 }
开发者ID:dullcat,项目名称:leetcode_csharp,代码行数:26,代码来源:Q092_ReverseLinkedListII.cs

示例10: AddTwoNumbers

        public ListNode<int> AddTwoNumbers(ListNode<int> l1, ListNode<int> l2)
        {
            ListNode<int> current = new ListNode<int>(-1);
            ListNode<int> head = current;

            int carry = 0;
            while (l1 != null || l2 != null || carry > 0)
            {
                int result = carry;
                if (l1 != null)
                {
                    result += l1.Val;
                    l1 = l1.Next;
                }
                if (l2 != null)
                {
                    result += l2.Val;
                    l2 = l2.Next;
                }

                current.Next = new ListNode<int>(result % 10);
                current = current.Next;
                carry = result / 10;
            }

            return head.Next;
        }
开发者ID:bluesteyes,项目名称:LeetSharp,代码行数:27,代码来源:Q002_AddTwoNumbers.cs

示例11: Partition

        public ListNode<int> Partition(ListNode<int> head, int x)
        {
            if (head == null)
                return null;

            ListNode<int> first = new ListNode<int>(-1);
            ListNode<int> second = new ListNode<int>(-1);
            ListNode<int> originalFirst = first;
            ListNode<int> originalSecond = second;

            while (head != null)
            {
                if (head.Val < x)
                {
                    first.Next = head;
                    first = first.Next;
                }
                else
                {
                    second.Next = head;
                    second = second.Next;
                }
                head = head.Next;
            }
            second.Next = null;
            first.Next = originalSecond.Next;

            return originalFirst.Next;
        }
开发者ID:bluesteyes,项目名称:LeetSharp,代码行数:29,代码来源:Q086_PartitionList.cs

示例12: AddTwoNumbers

 public ListNode<int> AddTwoNumbers(ListNode<int> l1, ListNode<int> l2)
 {
     // add l2 on to l1
     ListNode<int> current = new ListNode<int>(0);
     var answer = current;
     int toAdd = 0;
     while (true)
     {
         current.Val += toAdd;
         if (l1 != null)
         {
             current.Val += l1.Val;
             l1 = l1.Next;
         }
         if (l2 != null)
         {
             current.Val += l2.Val;
             l2 = l2.Next;
         }
         toAdd = current.Val / 10;
         current.Val = current.Val % 10;
         if (l1 != null || l2 != null || toAdd > 0)
         {
             current.Next = new ListNode<int>(0);
             current = current.Next;
         }
         else
         {
             break;
         }
     }
     return answer;
 }
开发者ID:dullcat,项目名称:leetcode_csharp,代码行数:33,代码来源:Q002_AddTwoNumbers.cs

示例13: Process

        public static ListNode Process(ListNode head, uint k)
        {
            if (head == null || k == 0)
            {
                return null;
            }

            ListNode ahead = head;
            for (int i = 0; i < k - 1; ++i)
            {
                if (ahead.Next != null)
                {
                    ahead = ahead.Next;
                }
                else
                {
                    return null;
                }
            }

            ListNode behind = head;
            while (ahead.Next != null)
            {
                ahead = ahead.Next;
                behind = behind.Next;
            }

            return behind;
        }
开发者ID:eriolchan,项目名称:problem,代码行数:29,代码来源:KthNodeFromEnd.cs

示例14: MergeKLists1

        public ListNode MergeKLists1(ListNode[] lists)
        {
            ListNode result = new ListNode(0);
            ListNode pointer = result;

            MinHeap mh = new MinHeap();
            foreach (ListNode node in lists)
            {
                if (node != null)
                {
                    mh.Add(node);
                }
            }

            while (mh.Count > 0)
            {
                ListNode node = mh.PopMin();
                if (node.next != null)
                {
                    mh.Add(node.next);
                }
                pointer.next = node;
                pointer = pointer.next;
            }

            return result.next;
        }
开发者ID:husthk986,项目名称:BlackSwan,代码行数:27,代码来源:_23MergedKSortedList.cs

示例15: Main

	public static void Main()
	{
		//test#1: null
		ListNode head = null;
		Solution.display(Solution.removeNodes(head,5)); //empty
		//test#2: 1
		head = new ListNode(1);
		Solution.display(Solution.removeNodes(head,5)); //1->
		//test#3: 5
		head = new ListNode(5);
		Solution.display(Solution.removeNodes(head,5)); //empty
		//test#4: 1->2
		head = new ListNode(1);
		head.next = new ListNode(2);
		Solution.display(Solution.removeNodes(head,5)); //1->2->
		//test#5: 5->5->5
		head = new ListNode(5);
		head.next = new ListNode(5);
		head.next.next = new ListNode(5);
		Solution.display(Solution.removeNodes(head,5)); //empty
		//test#6: 1->2->5->5
		head = new ListNode(1);
		head.next = new ListNode(2);
		head.next.next = new ListNode(5);
		head.next.next.next = new ListNode(5);
		Solution.display(Solution.removeNodes(head,5)); //1->2->
		//test#7: 1->2->5->5->3
		head = new ListNode(1);
		head.next = new ListNode(2);
		head.next.next = new ListNode(5);
		head.next.next.next = new ListNode(5);
		head.next.next.next.next = new ListNode(3);
		Solution.display(Solution.removeNodes(head,5)); //1->2->3->
	}
开发者ID:siagung,项目名称:Qilu-leetcode,代码行数:34,代码来源:A174.RemoveNodesWithValue.cs


注:本文中的ListNode类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。