• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# ListNode类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: MergeTwoLinkedList

        private ListNode MergeTwoLinkedList(ListNode node1, ListNode node2)
        {
            ListNode result = new ListNode(0);
            ListNode pointer = result;

            while (node1 != null && node2 != null)
            {
                if (node1.val < node2.val)
                {
                    pointer.next = node1;
                    node1 = node1.next;
                }
                else
                {
                    pointer.next = node2;
                    node2 = node2.next;
                }
                pointer = pointer.next;
            }

            while (node1 != null)
            {
                pointer.next = node1;
                node1 = node1.next;
                pointer = pointer.next;
            }
            while (node2 != null)
            {
                pointer.next = node2;
                node2 = node2.next;
                pointer = pointer.next;
            }

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


示例17: RotateRight

        public ListNode<int> RotateRight(ListNode<int> head, int n)
        {
            if (head == null)
            {
                return null;
            }

            int totalLength = 1;
            ListNode<int> tail = head;
            while (tail.Next != null)
            {
                totalLength++;
                tail = tail.Next;
            }

            n %= totalLength;

            if (n == 0)
            {
                return head;
            }

            ListNode<int> p = head;
            for (int i = 0; i < totalLength - n - 1; i++)
            {
                p = p.Next;
            }

            ListNode<int> newHead = p.Next;
            p.Next = null;
            tail.Next = head;

            return newHead;
        }
开发者ID:bluesteyes,项目名称:LeetSharp,代码行数:34,代码来源:Q061_RotateList.cs


示例18: RotateRight

        public ListNode RotateRight(ListNode head, int k)
        {
            if (k <= 0 || head == null) { return head; }

            var ptr = new ListNode(-1);
            ptr.next = head;

            int lenght = 0;
            while (ptr.next != null)
            {
                ptr = ptr.next;
                lenght++;
            }
            ptr.next = head;

            var rest = lenght - k % lenght;
            for (int i = 0; i < rest; i++)
            {
                ptr = ptr.next;
            }

            head = ptr.next;
            ptr.next = null;
            return head;
        }
开发者ID:BigEgg,项目名称:LeetCode,代码行数:25,代码来源:061-RotateList.cs


示例19: InsertionSortList

 public ListNode InsertionSortList(ListNode head) {
     ListNode preHead = new ListNode(Int.MinValue);
     preHead.next = head;
     ListNode prev = null; //points to the node before curr
     ListNode curr = head;
     while(curr!=null){
         ListNode newPrev = curr; //if curr won't be moved, the prev for next pass
         ListNode newCurr = curr.next; //if curr won't be moved, the curr for next pass
         ListNode p = preHead;
         while(p.next!=curr){
             if(curr.val>=p.val && curr.val<=p.next.val){//curr needs ot be inserted between p and p.next
                 ListNode temp = curr.next; //store original curr.next
                 curr.next = p.next; //insert curr 
                 p.next = curr;
                 prev.next = temp; //after removing curr, needs to connect prev and original curr.next
                 newPrev = prev; //curr is moved so prev for next pass doesn't change
                 break;
             }
             p=p.next;
         }
         prev=newPrev; //if curr is not moved, prev won't change, otherwiese, prev points to curr
         curr=newCurr; //curr would always points to curr.next
     }
     return preHead.next;
 }
开发者ID:siagung,项目名称:Qilu-leetcode,代码行数:25,代码来源:A166.InsertionSortList.cs


示例20: DeleteNode

 public void DeleteNode(ListNode node)
 {
     if (node == null) return;
     node.val = node.next.val;
     node.next = node.next.next;
     return;
 }
开发者ID:einstenwolfgang,项目名称:leetcode-exercise,代码行数:7,代码来源:Delete+Node+in+a+Linked+List.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# ListObject类代码示例发布时间:2022-05-24
下一篇:
C# ListJobsRequestMarshaller类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap